Reputation: 2927
I have one compilation error that I cannot solved yet. It's about an action adding a new patient into mongo if he is not already in the database. First the model :
case class PatientData(id : String)
object PatientData {
implicit val PatientDataFormat = Json.format[PatientData]
}
The function searching the patient in mongo :
def findPatientById(mode : String, id : String) : Future[Option[PatientData]] = {
val collection = getPatientCollection(mode)
collection.find(Json.obj("id" -> id)).one[PatientData]
}
The Play action :
def create(patientId: String) = Action.async(parse.json) { request =>
val token = "dummy"
isAuthorized(token) match { // always return Some(thing)
case None => Future.successful(Unauthorized("Invalid token " + token))
case Some(_) =>
request.body.validate[PatientData] map {
patientData =>
findPatientById(mode,patientId) map { finded =>
finded match {
case Some(_) => Future.successful(NotAcceptable("The patient is already exist."))
case None =>
Logger.info("Create the patient .. ")
Future.successful(Created)
}
}
} getOrElse {
Future.successful(BadRequest)
}
}
I know that I can solve this problem using a call to Await.result
in the function findPatientById
but I want avoid this solution and let the Future
doing his job. The problem is that I get a compilation error :
[error] /home/afk/git/bioserenity/bioserenity-backend/app/controllers/MedataController.scala:90: type mismatch;
[error] found : scala.concurrent.Future[Object]
[error] required: scala.concurrent.Future[play.api.mvc.Result]
[error] } getOrElse {
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
Anyone have an idea to solve this issue ?
Upvotes: 0
Views: 663
Reputation: 3131
You should probably try to use
findPatientById(mode,patientId) flatMap { ...
instead of original line. Here, the map
call is replaced by flatMap
so that the instance returned by that block of code is Future[Something]
rather than Future[Future[Something]]
.
Upvotes: 2