繁体   English   中英

使用Play with Scala类型不匹配映射Future [Seq [model]]

[英]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]] mapFuture[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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM