3
votes

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]

1
How about simply changing the last line to the call to map?Sardtok
@Sardtok Changing the last line to what?unleashed
Remove the val declaration. See Marth's answer below.Sardtok

1 Answers

7
votes

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.