scala返回Future [Unit]而不是Future [ContentComponentModel] [英] scala Returns Future[Unit] instead of Future[ContentComponentModel]

查看:95
本文介绍了scala返回Future [Unit]而不是Future [ContentComponentModel]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

  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)))

  }

我收到此错误消息:

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

The .list() method should return a Future[ContentComponentModel]

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

在这种情况下我犯了什么错误?

whats my mistake in this case?

谢谢

推荐答案

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

Your contentComponentsFuture should be of type Seq[ContentComponentModel]. In this case You should move

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

循环后仅进入映射表达式(异步).

just into the map expression (which is async) after loop.

它看起来应该像这样:

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)))
}

}

这篇关于scala返回Future [Unit]而不是Future [ContentComponentModel]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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