我有以下代码:
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()方法应该返回一个FutureContentComponentModel
def list(): Future[Seq[ContentComponentModel]] = db.run {
我在这个案子里犯了什么错误?
谢谢
发布于 2017-08-23 11:27:38
您的contentComponentsFuture应该是SeqContentComponentModel类型的。在这种情况下,你应该移动
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)))
}}
https://stackoverflow.com/questions/45835922
复制相似问题