1

以下控制器操作导致错误:

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

动作中的每个 Future 都应该是Ok(),所以我不明白为什么 scala 无法正确解析未来。

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")
  }
}

完整的错误信息:

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])
4

1 回答 1

0

我不确定在哪里,但我的案例结构似乎没有涵盖此操作中的一个案例。我对此进行了重构,现在它工作正常:

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")
  }
}
于 2013-10-28T19:19:49.577 回答