【问题标题】:Playframework 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]
【发布时间】:2015-03-13 05:27:47
【问题描述】:

我正在尝试在 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]])

【问题讨论】:

    标签: mongodb scala compiler-errors playframework-2.3


    【解决方案1】:

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

    所以改变,bsonIds.map { ids =&gt;bsonIds.flatMap{ ids =&gt;

    【讨论】:

    • 您好@SKarthik 再次感谢您。这对我来说是工作。请建议我,我从哪里学得好,Play和Scala?
    • 请参阅此链接以获取 scala:danielwestheide.com/scala/neophytes.html。还可以了解隐式、Monads 以及对 monads 的理解
    • 感谢@S Karthik,我在哪里找到了有关 Play 的知识?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多