3

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]

unleashed
  • 915
  • 2
  • 16
  • 35

1 Answers1

7

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.

Ryan
  • 7,227
  • 5
  • 29
  • 40