Play + Scala + Reactivemongo + Rest Query with two parameters

I have uploaded a typical "modern-web-template" application that implements a red app with game + scala + reactivemongo

I was trying to add new functionality. I want to be able to call url with 2 parameters like this

localhost:9000/users?dni&30000000

      

first I added this route to the routes file

GET     /users                      @controllers.Users.findUsersParams(tipoDocumento: String ?= "", numeroDocumento:  String ?= "")

      

Then I added this method to the controller

def findUsersParams(tipoDocumento: String, numeroDocumento: String) = Action.async {
// let do our query
val cursor: Cursor[User] = collection.
  // find all
  find(Json.obj("tipoDocumento" -> tipoDocumento, "numeroDocumento" -> numeroDocumento)).
  // sort them by creation date
  sort(Json.obj("created" -> -1)).
  // perform the query and get a cursor of JsObject
  cursor[User]

// gather all the JsObjects in a list
val futureUsersList: Future[List[User]] = cursor.collect[List]()

// transform the list into a JsArray
val futurePersonsJsonArray: Future[JsArray] = futureUsersList.map { users =>
  Json.arr(users)
}
// everything ok! Let reply with the array
futurePersonsJsonArray.map {
  users =>
    Ok(users(0))
}
}

      

I am unable to return the expected result, which should be a single user, instead I am getting all users inside the collection

+3


source to share


1 answer


There is an error in your request, it should be

http://localhost:9000/users?tipoDocumento=dni&numeroDocumento=30000000

      



Other than this code, it looks fine and should work.

+1


source







All Articles