我正在使用reactivemongo进行测试
在我的控制器中我有这个:
package controllers
import models._
import models.JsonFormats._
import play.modules.reactivemongo.MongoController
import scala.concurrent.Future
import reactivemongo.api.Cursor
import org.slf4j.{LoggerFactory, Logger}
import javax.inject.Singleton
import play.api.mvc._
import reactivemongo.api.collections.default.BSONCollection
import reactivemongo.bson._
@Singleton
class Users extends Controller with MongoController {
private final val logger: Logger = LoggerFactory.getLogger(classOf[Users])
val collection = db[BSONCollection]("users")
// list all articles and sort them
def list = Action.async { implicit request =>
// get a sort document (see getSort method for more information)
val sort = getSort(request)
// build a selection document with an empty query and a sort subdocument ('$orderby')
val query = BSONDocument(
"$orderby" -> sort,
"$query" -> BSONDocument())
val activeSort = request.queryString.get("sort").flatMap(_.headOption).getOrElse("none")
// the cursor of documents
val found = collection.find(query).cursor[User]
// build (asynchronously) a list containing all the articles
found.collect[List]().map { users =>
Ok(views.html.admin.list(users, activeSort))
}.recover {
case e =>
e.printStackTrace()
BadRequest(e.getMessage())
}
}
...........
}
在我的模型中我有这个:
包装模型
import reactivemongo.bson._
case class User(
nickName: String,
email: String,
password: String,
active: Boolean
)
object JsonFormats {
import play.api.libs.json.Json
// Generates Writes and Reads for Feed and User thanks to Json Macros
implicit val userFormat = Json.format[User]
}
编译项目时,返回以下错误:
无法找到参数阅读器的隐含值:reactivemongo.bson.BSONDocumentReader [models.User]
这一行是问题所在:
val found = collection.find(query).cursor [User]
有谁可以告诉我哪里错了或我错过了什么?
答案 0 :(得分:6)
您没有定义隐式处理程序来将模型类映射到BSONDocument
。您可以自己实施,或者就像您对JsonFormats
所做的那样,您可以使用macros provided by ReactiveMongo。
object BsonFormats {
import reactivemongo.bson.Macros
implicit val userFormat = Macros.handler[User]
}
或者,您可以使用Play-ReactiveMongo提供的BSONCollection
代替JSONCollection
,使用您已定义的JSON格式执行映射。
答案 1 :(得分:0)
对我来说,即使在我声明了bson
和json
格式的含义之后,我仍然会收到错误。我需要做的就是导入这个:
import reactivemongo.api.commands.bson.BSONCountCommandImplicits._