elarib
elarib

Reputation: 674

type mismatch scala.concurrent.Future with Slick & Play Framework

Hello,

    def getMessages(code:String,offset:Option[Int]) = Action.async(parse.json){request =>

    val forPC = (request.body \ "forPC").asOpt[Boolean]

    Talk.findByCode(code).map(_ match{
        case Success(talk) =>
            val talkId = talk.id
            Message.getMessages(talkId=talkId,offset=offset.getOrElse(0), forPC ).map(_ match {
                case Seq(Tables.Messages) => Ok("Cool")
                case    _ => Ok("No")
            })
        case Failure(e) =>
            Logger.error(e.getMessage)
            NotAcceptable(Json.toJson(Map(
                "error" -> "failed"
            )))
    })

And in the Model i have :

// talks by Code
  def findByCode(code: String, conferenceid : Int) = {
    val query = talks.filter(talk => talk.conferenceId === conferenceid && talk.code === code)
    db.run(query.result.head.asTry)
  }


    def getMessages(talkId:Int ,offset:Int, forPC: Option[Boolean]) = {
    val forPCVal = forPC.getOrElse(false)
    //ordering by talkId because it's faster than timestamp
    val query = messages.filter(msg => msg.talkId === talkId && msg.forpc === forPCVal ).sortBy(_.idmessage.desc).drop(offset).take(10).result
    db.run(query)
}

So Play is waiting for Result ( Action ) , and it displays this error :

type mismatch;
 found   : scala.concurrent.Future[play.api.mvc.Result]
 required: play.api.mvc.Result

and this :

enter image description here

Can anyone explain to why this error and give me some hints to solve it ?

Thank you

Upvotes: 0

Views: 280

Answers (1)

Valentin Kasas
Valentin Kasas

Reputation: 213

It seems that your Message.getMessages returns a Future[Something] which in turn make your whole match block attempt to return a Future[Result] in the Success case and a Result in the Failure case.

You should try something like the following (notice the flatMap that makes sure you end up with Future[Result] and not a Future[Future[Result]])

Talk.findByCode(code).flatMap(_ match{
    case Success(talk) =>
        val talkId = talk.id
        Message.getMessages(talkId=talkId,offset=offset.getOrElse(0), forPC ).map(_ match {
            case Seq(Tables.Messages) => Ok("Cool")
            case    _ => Ok("No")
        })
    case Failure(e) =>
        Logger.error(e.getMessage)
        Future.successful(NotAcceptable(Json.toJson(Map(
            "error" -> "failed"
        ))))
})

Upvotes: 0

Related Questions