我有一个用Play和ReactiveMongo编写的应用程序,我想要:
firebaseRef1.on('value', (snap) => console.log(snap.val()));
firebaseRef2.on('value', (snap) => console.log(snap.val()));
firebaseRef3.on('value', (snap) => console.log(snap.val()));
文档插入MongoDB的操作。我有这个工作代码:
landingPage
此代码工作正常。但是,出于好奇,我想知道如何访问// Insert the landing page and wait for it to be inserted, so we can then get the new count of landing pages.
val futures = for {
wr <- landingPagesCollection.insert(landingPage)
count <- landingPagesCollection.count()
} yield count
futures.map { (count: Int) =>
Created(Json.obj(
"created" -> true,
"landingPage" -> landingPage.toJson,
"count" -> count
))
}
(WriteResult)
值。当我将代码更改为:
wr
我收到以下错误消息:
有人可以解释如何在地图功能中访问val futures = for {
wr <- landingPagesCollection.insert(landingPage)
count <- landingPagesCollection.count()
} yield (wr, count)
futures.map { (wr: WriteResult, count: Int) =>
Created(Json.obj(
"created" -> true,
"message" -> s"You created landing page ${landingPage.name} (${landingPage.jobNumber}). The Git URI is '${landingPage.gitUri}'.",
"landingPage" -> landingPage.toJson,
"count" -> count,
"writeResult" -> wr
))
}
吗?
答案 0 :(得分:8)
尝试更改
futures.map { (wr: WriteResult, count: Int) =>
为:
futures.map { case (wr: WriteResult, count: Int) =>
或只是:
futures.map { case (wr, count) =>
希望这有帮助
答案 1 :(得分:6)
如果函数接受2个参数而不是元组
,则使用的语法有效list.map((a: Int, b: String) => ...)
如果你需要一个元组,你可以这样做:
list.map(tuple: (Int, String) => {
val (num, str) = tuple
...
})
或按照接受的答案中的建议传递部分功能。它将与参数匹配(它与元组匹配)并允许您稍后使用提取的值
list.map { case (a: Int, b: String) => ... }
请注意,在这种情况下,需要花括号。
可以删除类型注释,因为它们可以由编译器推断。