简体   繁体   English

reactmongo,找不到参数读取器的隐式值

[英]reactivemongo, could not find implicit value for parameter reader

I'm doing tests with reactivemongo 我正在用reactmongo做测试

In my controller I have this: 在我的控制器中,我有:

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())
    }
  }

  ...........

}

and in my model i have this: 在我的模型中,我有这个:

package models 包装型号

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]

}

When I compile the project returns the following error: 当我编译项目时,返回以下错误:

could not find implicit value for parameter reader: reactivemongo.bson.BSONDocumentReader[models.User] 找不到参数读取器的隐式值:reactmongo.bson.BSONDocumentReader [models.User]

in this line is the problem: 这是问题所在:

val found = collection.find(query).cursor[User] 找到的val = collection.find(query).cursor [User]

Can anyone tell me where I'm wrong or what I'm missing please? 谁能告诉我哪里错了或我想念什么?

You have no implicit handler defined to map your model class to a BSONDocument . 您没有定义将模型类映射到BSONDocument隐式处理程序。 You can implement it yourself, or, just like you did for the JsonFormats , you could use the macros provided by ReactiveMongo . 您可以自己实现它,或者就像您对JsonFormats所做的JsonFormats ,可以使用ReactiveMongo提供

object BsonFormats {
  import reactivemongo.bson.Macros

  implicit val userFormat = Macros.handler[User]

}

Alternatively, instead of the BSONCollection , you could use the JSONCollection provided by Play-ReactiveMongo to perform your mapping using the JSON format that you have already defined. 另外,代替BSONCollection ,你可以使用JSONCollection提供播放ReactiveMongo执行使用您已经定义了JSON格式的映射。

For me, I still get the error even after I have declared the implicits for both bson and json format. 对我来说,即使我已经声明了bsonjson格式的隐式内容,我仍然会收到错误消息。 What I need to do is just import this: 我需要做的就是导入此:

import reactivemongo.api.commands.bson.BSONCountCommandImplicits._

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM