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