游戏框架 2.3x:scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent]

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

我正在尝试 运行 async 游戏框架中的操作并使用反应式 mongo 进行数据库操作。反应性 mongo return 导致 Future 个对象。但是当我 return 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 是未来,futureVideos 也是未来,所以最后一行将是 Future[Future[_]]。

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