Scala Play Action.async cant resolve Ok as mvc.AnyContent

自闭症网瘾萝莉.ら 提交于 2020-01-03 02:46:10

问题


The following controller action cause the error:

(block: => scala.concurrent.Future[play.api.mvc.SimpleResult])play.api.mvc.Action[play.api.mvc.AnyContent]  cannot be applied to (scala.concurrent.Future[Object])". 

Every Future inside the action should be Ok() so I don't understand why scala can't resolve the futures correctly.

def form = Action.async {
  val checkSetup: Future[Response] = EsClient.execute(new IndexExistsQuery("fbl_indices"))
  checkSetup map {
    case result if result.status == 200 => Ok("form")
    case result => {
      val createIndexResult: Future[Response] = EsClient.execute(new FillableSetupQuery())
      createIndexResult map {
        indexCreated => Ok("form").flashing("success" -> "alles tutti")
      } recover {
        case e: Throwable => Ok("form error").flashing("message" -> "error indexerzeugung")
      }
    }
  } recover {
    case e: Throwable => Ok("form error").flashing("message" -> "error index query")
  }
}

Complete Error messages:

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.SimpleResult])play.api.mvc.Action[A] <and>   (block: play.api.mvc.Request[play.api.mvc.AnyContent] => scala.concurrent.Future[play.api.mvc.SimpleResult])play.api.mvc.Action[play.api.mvc.AnyContent] <and>   (block: => scala.concurrent.Future[play.api.mvc.SimpleResult])play.api.mvc.Action[play.api.mvc.AnyContent]  cannot be applied to (scala.concurrent.Future[Object])

回答1:


I am not sure where but it seems that there was a case in this action that was not covered by my case structure. I refactored the action to this and it works fine now:

def form = Action.async {
  EsClient.execute(new IndexExistsQuery("fbl_indices")) flatMap { 
    index =>  if (index.status == 200) Future.successful(Ok("form"))
    else {
      EsClient.execute(new FillableSetupQuery()) map {
        indexCreated => Ok("form").flashing("success" -> "alles tutti")
      } recover {
        case e: Throwable => Ok("form error").flashing("error" -> "error indexerzeugung")
      }
    }
  } recover {
    case e: Throwable => Ok("form error").flashing("error" -> "error index query")
  }
}


来源:https://stackoverflow.com/questions/19642643/scala-play-action-async-cant-resolve-ok-as-mvc-anycontent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!