如何使用 MongoListField 返回列表

发布于 2024-11-26 00:30:55 字数 907 浏览 0 评论 0原文

我正在学习 lift 和 mongodb。我遇到了如下问题。为了简单起见,我将绕过一些代码。代码如下:

object User extends User with MetaMegaProtoUser[User] {

}

class User extends MegaProtoUser[User] {
  def meta = User

  //record the post list that user like
  object likePostList extends MongoListField[User, ObjectId](this)

  def test()
  {
    val list:  = this.likePostList
    println(list.length)
  }
}

error: value length is not a member of object User.this.likePostList
this.likePostList.length

我可以将 ObjectId 数据存储在 MongoDB 中。但 MongoListField 不返回列表。为什么?如何将其用作列表。

我尝试将类型大小写添加到 List[OjbectId] 或 List[String] 但没有运气。

val list: List[ObjectId] = this.likePostList.asInstanceOf[List[Object]]

出现错误: java.lang.ClassCastException: com.cosiin.model.User$likePostList$ 无法转换为 scala.collection.immutable.List

我认为我使用 MongoListField 的方式错误。但我不知道如何使用它。
有人可以帮忙吗?谢谢

I am learning lift and mongodb. I encountered a problem as following. I will bypass some code for simplicity. Here is the code:

object User extends User with MetaMegaProtoUser[User] {

}

class User extends MegaProtoUser[User] {
  def meta = User

  //record the post list that user like
  object likePostList extends MongoListField[User, ObjectId](this)

  def test()
  {
    val list:  = this.likePostList
    println(list.length)
  }
}

error: value length is not a member of object User.this.likePostList
this.likePostList.length

I can store the ObjectId data in MongoDB. But the MongoListField does not return a list. Why? How to use it as a List.

I try to add type case it to List[OjbectId] or List[String] but without luck.

val list: List[ObjectId] = this.likePostList.asInstanceOf[List[Object]]

Got error:
java.lang.ClassCastException: com.cosiin.model.User$likePostList$ cannot be cast to scala.collection.immutable.List

I think I am using MongoListField the wrong way. But I do not know how to use it.
Can anyone help? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

口干舌燥 2024-12-03 00:30:55

您在电梯记录中声明的字段是字段对象,而不是字段中包含的实际值。为了访问实际值,您需要调用:

this.likePostList.get

或者如果该字段是可选的

this.likePostList.valueBox

,则返回一个类似 scala.Option 的对象。

如果您仔细想想,这非常有意义,因为您在键入时实际上将 likePostList 声明为 MongoListField 的实例:

object likePostList extends MongoListField[User, ObjectId](this)

相反

val likePostList: List[ObjectId] = ...

与Scala 没有任何神奇之处 执行自动将其转换为List[ObjectId]。等同于:

class Foo {
  val bar = 3
  object baz { val greeting = "hello" }
}

val foo = new Foo
println(foo.bar) // prints 3
println(foo.baz) // prints something like Foo$baz$@1d981b6a
println(foo.baz.greeting) // prints "hello"

PS,在旧版本的 Lift 中,get 被称为 is

Fields you declare in Lift Records are field objects, not the actual values contained within the field. In order to access the actual value you need to call:

this.likePostList.get

or if the field is optional

this.likePostList.valueBox

which returns a scala.Option like object.

If you think of it, this makes very much sense because you're actually declaring likePostList to be an instance of MongoListField when you type:

object likePostList extends MongoListField[User, ObjectId](this)

as opposed to

val likePostList: List[ObjectId] = ...

there's no magic Scala can do to automatically convert that to List[ObjectId]. It's the same as:

class Foo {
  val bar = 3
  object baz { val greeting = "hello" }
}

val foo = new Foo
println(foo.bar) // prints 3
println(foo.baz) // prints something like Foo$baz$@1d981b6a
println(foo.baz.greeting) // prints "hello"

P.S. in older versions of Lift, get was called is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文