John Doe John Doe - 3 months ago 75
Scala Question

Type mismatch when upgrading to ReactiveMongo 0.12.RC3

I am currently upgrading to 0.12.RC3 in hope of fixing the following issue I am experiencing. After upgrading, I received a deprecation warning for the

collect
method.

So I moved from:

def find(query: JsObject = Json.obj())(implicit reader: Reads[T]): Future[List[T]] = {
collection.flatMap(_.find(query).cursor[T](ReadPreference.nearest).collect[List]())
}


To:

def find(query: JsObject = Json.obj())(implicit reader: Reads[T]): Future[List[T]] = {
collection.flatMap(_.find(query).cursor[T](ReadPreference.nearest).collect[List](Int.MaxValue, Cursor.FailOnError()))
}


However, unfortunately I get the following error:


Type mismatch, expected: (JSONCollection) => Future[NotInferedS],
actual: (JSONCollection) => Any

Answer

I think you are missing some compiler message, and should see something like:

  (maxDocs: Int,stopOnError: Boolean)(implicit cbf: scala.collection.generic.CanBuildFrom[List[_],T,List[T]], implicit ec: scala.concurrent.ExecutionContext)scala.concurrent.Future[List[T]] <and>
  (maxDocs: Int,err: reactivemongo.api.Cursor.ErrorHandler[List[T]])(implicit cbf: scala.collection.generic.CanBuildFrom[List[_],T,List[T]], implicit ec: scala.concurrent.ExecutionContext)scala.concurrent.Future[List[T]]
 cannot be applied to (Int, reactivemongo.api.Cursor.ErrorHandler[Any])
Error occurred in an application involving default arguments.
           collection.flatMap(_.find(query).cursor[T](ReadPreference.nearest).collect[List](Int.MaxValue, Cursor.FailOnError()))

It means that in such case if you want to use the new collect instead of the deprecated one, you need to properly annotate the ErrorHandler (there FailOnError) with the result type: FailOnError[List[T]]

def find(query: JsObject = Json.obj())(implicit reader: Reads[T]): Future[List[T]] = collection.flatMap(_.find(query).cursor[T](ReadPreference.nearest).collect[List](Int.MaxValue, Cursor.FailOnError[List[T]]()))