Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala - Unit found required: play.api.mvc.SimpleResult

I'm really confused here, i'm probably missing something obvious so it would be great if someone could point me into the right direction.

I've got this following function that returns a future of a SimpleResult type but it has a unit instead . I'm not sure why it is saying this as i am mapping the futureList result

  def find(key: String, value: String) = Future[SimpleResult] {

  val cursor: Cursor[JsObject] = coll.
    find(Json.obj(key -> value)).
    cursor[JsObject]

  val futureList: Future[List[JsObject]] = cursor.collect[List]()

  val futureResult: Future[SimpleResult] = futureList.map { item =>

    if(item.isEmpty) {

        Ok(JsArray()).as(JSON)
    }
    else 
         Ok(Json.toJson(item))     
 }
} 

Edit

I've changed it to the following from the advice given by Marth

  def find(key: String, value: String) = Future[SimpleResult] {

  val cursor: Cursor[JsObject] = coll.
    find(Json.obj(key -> value)).
    cursor[JsObject]

  val futureList: Future[List[JsObject]] = cursor.collect[List]()

   futureList.map { item =>  Ok(Json.toJson(item))    }

}

Eclipse is warning about the following

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

Although the function is meant to return a scala.concurrent.Future[play.api.mvc.SimpleResult]

like image 414
unleashed Avatar asked Dec 03 '25 22:12

unleashed


1 Answers

It's a syntax error.

def find(key: String, value: String): Future[SimpleResult] = {

  val cursor: Cursor[JsObject] = coll.
    find(Json.obj(key -> value)).
    cursor[JsObject]

  val futureList: Future[List[JsObject]] = cursor.collect[List]()

  futureList.map { item =>  Ok(Json.toJson(item))    }

}

Note the ":" and "=" symbols in the first line. Without the colon, you're actually just calling Future.apply[SimpleResult] which takes a single parameter: a function which should return a SimpleResult. That's why you get a type mismatch: you're returning a Future within the body of a Future.

like image 142
Ryan Avatar answered Dec 06 '25 12:12

Ryan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!