Play framework error: type mismatch - found scala.concurrent.Future[play.api.mvc.Result] required: play.api.mvc.Result

时间:2019-03-19 15:19:04

标签: scala play-framework-2.7

I have the following code in my Controller in play-scala project.

def removeItemFromShoppingBag: Action[JsValue] = Action(parse.json) {
    implicit request =>
      request.body.validate[RemoveItem]
       .fold(
         errors =>
            BadRequest(Json.obj("status" -> "KO", "message" -> JsError.toJson(errors))),
         removeItem => {
           productService.retrieve(removeItem.itemID).flatMap {
             case Some(item) => productInTheBagService.retrieve(item.id).flatMap {
               case Some(itemInTheBag) =>
                 Future.successful(
                   Ok(ApiResponse("shopping.products", Messages("request.ok")))
                 )
               case None =>
                 Future.successful(
                   BadRequest(ApiResponse("home.shopping.item.not.available",
                      Messages("shopping.item.not.available")))
          )
        }

        case None =>
          Future.successful(
            BadRequest(ApiResponse("home.shopping.item.not.available",
              Messages("shopping.item.not.available")))
          )
        }
      }
    )
   }

I get the following error when compiling:

Controller.scala:236:62: type mismatch;
[error]  found   : scala.concurrent.Future[play.api.mvc.Result]
[error]  required: play.api.mvc.Result
[error]           productService.retrieve(removeItem.itemID).flatMap {
[error]                                                              ^
[error] one error found

I've read the answers to a similar question like this one: Playframework: Type mismatch found scala.concurrent.Future[play.api.mvc.Result] required: play.api.mvc.Result But it didn't help.

I shall be very grateful for any help.

1 个答案:

答案 0 :(得分:1)

您需要Action.async(parse.json)才能返回Future