mongodb - Playframework 2.3x : scala. concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent]

标签 mongodb scala compiler-errors playframework-2.3

我正在尝试运行 async游戏框架中的操作并使用响应式 mongo 进行数据库操作。 react 性 mongo 返回结果 Future对象。但是当我返回 Future 结果时,我得到了编译时错误。 Play 和 Scala 对我来说是新的,但仍然没有得到实际问题是什么?我有另一个代码,代码工作成功。

工作代码:

def dashboard = Action.async{
  logger.info("In dashboard controller method");

  var cursor: Cursor[Playlist] = playlistCollection.find(Json.obj("isPublished"-> true)).sort(Json.obj("htiCount"-> -1)).cursor[Playlist];
  val playlistList : Future[List[Playlist]] = cursor.collect[List]();
  playlistList.map { users => Ok(Json.toJson(users)) } 

}

错误代码:
def playlistVideos(plalistId: String) = Action.async{
  logger.info("In playlistVideos controller method");

  var objectIds: List[JsObject] = CustomUtility.convertStringIdToJsonCriteria(List{plalistId});
  var query = Json.obj("_id" -> Json.obj("$in" -> objectIds))
  val cursor: Cursor[Playlist] = playlistCollection.find(query).cursor[Playlist];
  val futurePlayList: Future[Option[Playlist]] = cursor.headOption;
  var bsonIds: Future[List[BSONObjectID]] = futurePlayList.map { option => {
      var playlist: Playlist = option.get;
      var optionVideos: Option[List[BSONObjectID]] = playlist.linkedVideoIds;
      optionVideos.getOrElse(List[BSONObjectID]());
  }}

  bsonIds.map { ids => {
     var videosList: List[String] = ids.map { idObj => idObj.stringify }
     var videosIds:List[JsObject] = CustomUtility.convertStringIdToJsonCriteria(videosList);

     query = Json.obj("linkedVideoIds" -> Json.obj("$in" -> objectIds));
     val videosCursor: Cursor[Video] = videosCollection.find(query).cursor[Video];
     val futureVideos: Future[List[Video]] = videosCursor.collect[List]();

     futureVideos.map { videos => Ok(Json.toJson(videos)) }
  }}
}

以下是我的编译时错误。
overloaded method value async with alternatives: [A](bodyParser: play.api.mvc.BodyParser[A])(block: play.api.mvc.Request[A] ⇒ scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[A] <and> (block: play.api.mvc.Request[play.api.mvc.AnyContent] ⇒ 

scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent] <and> (block: ⇒ scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent] cannot be applied to (scala.concurrent.Future[scala.concurrent.Future[play.api.mvc.Result]])

最佳答案

bsonIds 是 future ,futureVideos 也是 future ,所以最后一行是 Future[Future[_]]。

所以改变,bsonIds.map { ids =>bsonIds.flatMap{ ids =>

关于mongodb - Playframework 2.3x : scala. concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29025456/

相关文章:

angularjs - 使用 Angular.js 和 Node.js 将当前集合插入到另一个服务器中

mongodb - MongoDB 和 CouchDB 是完美的替代品吗?

Json 到 Kotlin 数据类

Scala:具有嵌套案例类的泛型函数

c - 在 C 中用字符串插入二叉树

python - 使用 asyncio_mongo 在 mongodb 上插入

performance - erlang vs jvm (scala) 递归性能

android - 任务 ':MyApp:dexDebug' 执行失败

css - rake Assets :precompile not working - Windows 7

Node .js/mongoDB : Return custom value if field is null