I have an application, written with Play and ReactiveMongo, where I want to:
- Have an action that inserts a
landingPage
document into MongoDB. - Insert the new landingPage and wait for that to insert.
- Count the new total number of landingPage documents and return that to the user.
I have this working code:
// 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
))
}
This code works fine. However, out of curiosity I want to know how to access the wr
(WriteResult)
value. When I changed the code to:
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
))
}
I get the following error messages:
Can anyone please explain how to access wr
in the map function?