Reputation: 6679
I downloaded the typesafe application "modern-web-template", which implements a crud application with play + scala + reactivemongo
I was trying to add a new functionality. I want to be able by calling a URL with two params 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's 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's ok! Let's reply with the array
futurePersonsJsonArray.map {
users =>
Ok(users(0))
}
}
I am not able to return the expected result that should be one user, instead, I am getting all the users inside the collection
Upvotes: 3
Views: 247
Reputation: 7735
There is an error in your Query, it should be
http://localhost:9000/users?tipoDocumento=dni&numeroDocumento=30000000
Other than that code seems fine, and should be working.
Upvotes: 1