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

时间:2015-03-13 05:27:47

标签: mongodb scala compiler-errors playframework-2.3

我正在尝试在Play框架中运行async操作并使用反应式mongo进行数据库操作。反应性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]])

1 个答案:

答案 0 :(得分:2)

bsonIds是未来和未来视频也是未来,所以最后一行将是Future [Future [_]]。

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