升级到 ReactiveMongo 0.12.RC3 时类型不匹配

Type mismatch when upgrading to ReactiveMongo 0.12.RC3

我目前正在升级到 0.12.RC3,希望修复我遇到的 following issue。升级后,我收到了 collect 方法的弃用警告。

所以我从:

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

收件人:

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

但是,不幸的是我收到以下错误:

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

我认为您遗漏了一些编译器消息,应该会看到如下内容:

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

这意味着在这种情况下,如果您想使用新的 collect 而不是已弃用的 collect,您需要使用结果类型: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]]()))