2016-03-30 12 views
0

私はまだ解決できないコンパイルエラーが1つあります。彼がデータベースにいない場合、新しい患者をmongoに追加するアクションです。まずモデル:Play Scala Framework:タイプミスマッチscala.concurrent.Future [オブジェクト] [エラー]必須:scala.concurrent.Future [play.api.mvc.Result]

case class PatientData(id : String) 

object PatientData { 
    implicit val PatientDataFormat = Json.format[PatientData] 
    } 

モンゴで患者を検索する機能:

def findPatientById(mode : String, id : String) : Future[Option[PatientData]] = { 
    val collection = getPatientCollection(mode) 
    collection.find(Json.obj("id" -> id)).one[PatientData] 
    } 

プレイアクション:

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) 
     } 
    } 

私はAwait.resultへの呼び出しを使用してこの問題を解決することができることを知っていますfindPatientByIdの機能では、私はこの解決方法を避けてFutureの仕事をしたいと思っています。問題は、コンパイルエラーが発生することです。

[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 

誰でもこの問題を解決するためのアイデアはありますか?

答えて

2

おそらく、代わりに元の行の

findPatientById(mode,patientId) flatMap { ... 

を使用するようにしてください。ここで、mapコールはflatMapに置き換えられ、そのコードブロックによって返されたインスタンスはFuture[Future[Something]]ではなくFuture[Something]になります。

+0

修正ありがとうございます:) – alifirat

関連する問題