我为我的Scala使用play-slick Play!虚拟休息API。因此,我不得不从多个表中提取记录。但是,它们是相互依存的,即Scala Play Future Interdependancy
Table_1 Table_2
id1 id2
id2
要提取TABLE_2记录我要取从TABLE_1记录,然后使用ID2从TABLE_2取。
我的控制器:
def getEntity(id : Long) = Action.async {
table1DAO.findById(id) map { t1 =>
t1 map { t1Entity =>
table2DAO.findById(t1Entity.id2) map { t2 =>
t2 map { t2Entity =>
Ok(Json.toJson(CombiningClass(t1Entity, t2Entity)))
} getOrElse { Ok(Json.toJson(t1Entity)) }
}
} getOrElse { NoContent }
}
}
编译后,我得到:
[error] DummyController.scala:17: overloaded method value async with alternatives:
[error] [A](bodyParser: play.api.mvc.BodyParser[A])(block: play.api.mvc.Request[A] => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[A] <and>
[error] (block: play.api.mvc.Request[play.api.mvc.AnyContent] => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent] <and>
[error] (block: => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent]
[error] cannot be applied to (scala.concurrent.Future[Object])
[error] def getEntity(id : Long) = Action.async {
[error] ^
[error] one error found
这里是我的DAO方法:
def findById(id : Long): Future[Option[A]] = {
db.run(tableQ.filter(_.id === id).result.headOption)
}
PS:我是很新的功能范式和斯卡拉,所以如果可以的话,请原谅我的无知。
试着注释顶部'map':'table1DAO.findById(id).map [结果] {t1 => ...}'以确保给出'Future [Result]'。附:您的代码的可读性将受益于使用理解。 – cchantep