scala返回Future [Unit]而不是Future [ContentComponentModel]

时间:2017-08-23 09:32:38

标签: scala playframework

我有以下代码:

  def getContentComponents: Action[AnyContent] = Action.async {
        val test = contentComponentDTO.list().map { contentComponentsFuture =>
          contentComponentsFuture.foreach(contentComponentFuture =>

            contentComponentFuture.typeOf match {
              case 1 =>
                println("blubb")
              case 5 =>
                contentComponentDTO.getContentComponentText(contentComponentFuture.id.get).map(
                  text => {
                    contentComponentFuture.text = text.text
                    println(text.text)
                    println(contentComponentFuture.text)
                  }
                )
            }
          )

        }

    Future.successful(Ok(Json.obj("contentComponents" -> test)))

  }

我收到了此错误消息:

enter image description here

.list()方法应返回Future [ContentComponentModel]

def list(): Future[Seq[ContentComponentModel]] = db.run {

在这种情况下我的错误是什么?

感谢

1 个答案:

答案 0 :(得分:1)

您的contentComponentsFuture应为Seq [ContentComponentModel]类型。在这种情况下你应该移动

Future.successful(Ok(Json.obj("contentComponents" -> test)))

在循环之后进入地图表达式(这是异步)。

它应该看起来像:

def getContentComponents: Action[AnyContent] = Action.async {
val test = contentComponentDTO.list().map { contentComponents =>
  contentComponents.foreach(contentComponentFuture =>
    contentComponentFuture.typeOf match {
      case 1 =>
        println("blubb")
      case 5 =>
        contentComponentDTO.getContentComponentText(contentComponentFuture.id.get).map(
          text => {
            contentComponentFuture.text = text.text
            println(text.text)
            println(contentComponentFuture.text)
          }
        )
    }
  )
  Future.successful(Ok(Json.obj("contentComponents" -> contentComponents)))
}

}