Play Framework Controller Handling Multiple Futures

早过忘川 提交于 2020-05-01 06:22:08

问题


I have a Play controller that takes in a Model object that comes in from the user interface. This model object is a User that I'm trying to insert into the database. But before I insert this User in the table, I check if there is a duplicate user that already exist for the given EMail address. If yes, I reject the request and if not I insert. I'm using Slick and Play framework and here is my controller code:

  def registerNewUser(user: User) = {
    dbService.registerNewUser(User.toUserRow(user))
      .map(userID => Ok(Json.obj("status" -> "ok", "userId" -> userID)))
      .recover { case ex => InternalServerError(Json.obj("error" -> s"${ex.getMessage}")) }
  }

  def createUser() = Action.async(parse.tolerantJson) { request =>
    request.body.validate[User].fold(
      errors => Future.successful {
        BadRequest(Json.obj("status" -> "error", "message" -> JsError.toJson(errors)))
      },
      user => {
        val userExists: Future[Boolean] = dbService.userExists(user.email)
        userExists.map(value => {
          if (value) UnprocessableEntity(Json.obj("status" -> "error", "message" -> s"user with email ${user.email.toString} already exists"))
          else registerNewUser(user)
        }).recover {
          case ex => InternalServerError(Json.obj("error" -> s"${ex.getMessage}"))
        }
      } // compilation failure on this line [[ Line X ]]
    )
  }

I do not see anything wrong with this approach, but my compiler is not happy. It complaints at Line X as:

Expression of type Future[Object] does not confirm to the expected type _X

What is actually the problem here?


回答1:


Btw shouldn't you use flatMap instead of map here?

userExists.map(value => {
          if (value) UnprocessableEntity(Json.obj("status" -> "error", "message" -> s"user with email ${user.email.toString} already exists"))
          else registerNewUser(user)
        })

registerNewUser returns Future right?

Maybe like this:

        val userExists: Future[Boolean] = dbService.userExists(user.email)
        userExists.flatMap(value => {
          if (value) Future.successful(UnprocessableEntity(Json.obj("status" -> "error", "message" -> s"user with email ${user.email.toString} already exists")))
          else registerNewUser(user)
        }).recover {
          case ex => InternalServerError(Json.obj("error" -> s"${ex.getMessage}"))
        }

?



来源:https://stackoverflow.com/questions/41875235/play-framework-controller-handling-multiple-futures

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