1

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?

Sanx
  • 223
  • 1
  • 6
  • 17

2 Answers2

7

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.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • I already declared the implicits for both json and bson but it still throw the same error – Zennichimaro Oct 16 '17 at 10:55
  • https://stackoverflow.com/questions/31142366/no-json-serializer-as-jsobject-found-for-type-play-api-libs-json-jsobject?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa#comment87919287_31145557 @Zennichimaro – franklin May 21 '18 at 17:55
0

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._

Zennichimaro
  • 5,236
  • 6
  • 54
  • 78