[英]Type mismatch mapping Future[Seq[model]] using Play with Scala
有时我仍然很难使用Play with Scala来映射Future ...
我试图将我的整个Supply DB表(即数据库中的所有耗材)传递给视图。
我尝试了两种不同的方法,但是都失败了...
以下是我尝试过的方法和遇到的错误。
有人可以帮我解决这个问题,并向我解释为什么两者都失败了吗?
先感谢您!
注意:调用supplyService.all
返回Future[Seq[Supply]]
。
第一次尝试
def index = SecuredAction.async { implicit request =>
supplyService.all map { supplies =>
Future.successful(Ok(views.html.supplies.index(request.identity, SupplyForm.form, supplies)))
}
}
第二次尝试
def index = SecuredAction.async { implicit request =>
val supplies = supplyService.all
Future.successful(Ok(views.html.supplies.index(request.identity, SupplyForm.form, supplies)))
}
没有Future.succesfull
第一个变体
supplyService.all.map( supplies => Ok(views.html.supplies.index(request.identity, SupplyForm.form, supplies)) )
由于您可以构造函数Seq[Supply] => Result
,因此可以通过函子接口轻松map
Future[Seq[Supply]]
map
到Future[Result]
。
Future也是monad ,因此您可以通过flatMap
方法使用Seq[Supply] => Future[Result]
。
但是Future.successfull
是monad单位,对于Future
许多monad来说,
mx.flatMap(f andThen unit) = mx.map(f)
所以你
ms.flatMap(supplies => Future.successfull(f(supplies)) =
ms.flatMap(f andThen Future.successfull) =
ms.map(f)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.