如何使用 MongoListField 返回列表
我正在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在电梯记录中声明的字段是字段对象,而不是字段中包含的实际值。为了访问实际值,您需要调用:
或者如果该字段是可选的
,则返回一个类似 scala.Option 的对象。
如果您仔细想想,这非常有意义,因为您在键入时实际上将
likePostList
声明为MongoListField
的实例:相反
与Scala 没有任何神奇之处 执行自动将其转换为
List[ObjectId]
。等同于: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:
or if the field is optional
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 ofMongoListField
when you type:as opposed to
there's no magic Scala can do to automatically convert that to
List[ObjectId]
. It's the same as:P.S. in older versions of Lift,
get
was calledis
.