Playframework: Type mismatch found scala.concurrent.Future[play.api.mvc.Result] required: play.api.mvc.Result [英] Playframework: Type mismatch found scala.concurrent.Future[play.api.mvc.Result] required: play.api.mvc.Result

查看:60
本文介绍了Playframework: Type mismatch found scala.concurrent.Future[play.api.mvc.Result] required: play.api.mvc.Result的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PlayFramework 的控制器中有以下代码:

I have the following code in my controller in PlayFramework:

  def auth = Action.async(parse.json) { request =>
    {

      val authRequest = request.body.validate[AuthRequest]
      authRequest.fold(
        errors => Future(BadRequest),
        auth => {
          credentialsManager.checkEmailPassword(auth.email, auth.password).map {

            case Some(credential: Credentials) => {

              sessionManager.createSession(credential.authAccountId).map { //Throws an error
                case Some(authResponse: AuthResponse) => Ok(Json.toJson(authResponse))
                case None => InternalServerError

              }

            }

            case (None) => Unauthorized
          }

        })
    }
  }

我在上面带有错误注释的行中收到以下错误:

I get the following error at the line with error comment above:

Type Mismatch:
[error]  found   : scala.concurrent.Future[play.api.mvc.Result]
[error]  required: play.api.mvc.Result
[error]               sessionManager.createSession(credential.authAccountId).map {

那里的 createSession 调用返回一个 Future[Option[Object]] 但我不知道如何解决这个问题.

The createSession call there returns a Future[Option[Object]] but I can't figure out how to fix this problem.

任何帮助将不胜感激.

推荐答案

简短回答:在 credentialsManager.checkEmailPassword(auth.email, auth.password).mapcase (None) 行中将 .map 更改为 .flatMap) =>未经授权case None =>未来(未经授权)

Short answer: Change .map to .flatMap in line credentialsManager.checkEmailPassword(auth.email, auth.password).map and case (None) => Unauthorized to case None => Future(Unauthorized)

说明:

credentialsManager.checkEmailPassword(auth.email, auth.password) 返回一个 Future[Option[Credentials]] 并且映射到它总是返回一个 Future 并在其中 sessionManager.createSession(credential.authAccountId) 也返回一个 Future 所以,credentialsManager.checkEmailPassword(auth.email, auth.password)Future[Future[something]] 为了避免这种情况,你可以改为 flatten 然后 map 它和它可以通过 flatmap

credentialsManager.checkEmailPassword(auth.email, auth.password) returns a Future[Option[Credentials]] and mapping on that will always return a Future and inside it sessionManager.createSession(credential.authAccountId) also returns a Future So, final outcome of credentialsManager.checkEmailPassword(auth.email, auth.password) was Future[Future[something]] to avoid such situations you can instead flatten it and then map it and it can be done in a single step by flatmap

这篇关于Playframework: Type mismatch found scala.concurrent.Future[play.api.mvc.Result] required: play.api.mvc.Result的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆