Sanx
Sanx

Reputation: 323

reactivemongo, could not find implicit value for parameter reader

I'm doing tests with reactivemongo

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]

in this line is the problem:

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

Can anyone tell me where I'm wrong or what I'm missing please?

Upvotes: 1

Views: 4291

Answers (2)

Zennichimaro
Zennichimaro

Reputation: 5306

For me, I still get the error even after I have declared the implicits for both bson and json format. What I need to do is just import this:

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

Upvotes: 0

Robby Cornelissen
Robby Cornelissen

Reputation: 97120

You have no implicit handler defined to map your model class to a BSONDocument. You can implement it yourself, or, just like you did for the JsonFormats, you could use the macros provided by 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.

Upvotes: 7

Related Questions