scala Returns Future[Unit] instead of Future[ContentComponentModel]

我的梦境 提交于 2020-01-06 07:21:33

问题


I have the following code:

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

  }

and I got this error message:

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

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

whats my mistake in this case?

thanks


回答1:


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.

It should looks something like:

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

}



来源:https://stackoverflow.com/questions/45835922/scala-returns-futureunit-instead-of-futurecontentcomponentmodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!