I am trying to find an available slug in mongodb using Play2 and reactivemongo. I came up with the following recursive method.
private def findSlug(base: String, index: Int): String = {
val slug: String = Slug({
base + "-" + index
})
for {
user <- findOne(BSONDocument("slug" -> slug))
} yield {
user match {
case None => slug
case Some(user) => findSlug(base, index+1)
}
}
}
I get the following error
found : scala.concurrent.Future[String]
required: String
user <- findOne(BSONDocument("slug" -> slug))
^
I played around a lot with changing return types, mapping the result of the yield, etc. but somehow I think there might be a far simpler, concise and correct solution. It all boils down to having a recursive function that calls another asynchronous function I think.
If I change the return type of findSlug to Future[String] I get
[error] found : scala.concurrent.Future[String]
[error] required: String
[error] case Some(user) => findSlug(base, index+1)
[error] ^
What would be the correct and idiomatic solution?