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

编程入门 行业动态 更新时间:2024-10-28 12:23:00
本文介绍了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).map 和 case (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]

本文发布于:2023-09-07 20:37:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466857.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:scala   concurrent   mismatch   Playframework   Type

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!