Reactivemongo with Scala를 사용하여 Play 2.3.2 응용 프로그램을 작성하고 있습니다. 내 데이터베이스에서 모든 사용자 데이터를 컬렉션에 저장합니다. 값이 방법은 나쁜 반환 형성 나쁜 경우reactivemongo를 사용한 비동기 재생 방법에서 오류가 발생했습니다.
- 방법은 request.body 개체에서
를 JSON 값을 :
지금 나는 다음과 같은 동작과 비동기 방법을 쓰고 있어요 요청 응답.
컬렉션에 이미 동일한 User 개체를 나타내는 문서가 들어 있으면이 메서드는 Ok 응답을 반환합니다.
- 그렇지 않으면이 메서드는 문서를 db에 삽입하고 Ok 응답을 반환합니다.
def addUser = CorsAction.async { request => val jsonData = request.body.asJson jsonData match { case Some(x) => val userId = x \ "id"; val userEmail = x \ "email"; (userId, userEmail) match { case (id: JsString, email: JsString) => val user = new User(id.as[String], Some(email.as[String])) //create a user Instance Users.find(Json.obj("id" -> user, "email" -> email)).toList.map{users: List[User] => val number = users.size //the number of user with the same id and email if(number == 0) Ok("The user already exists") Users.save(user).map{error => error match { case LastError(ok, _, _, _, _, _, _) => Ok //insert on the db complete sucessfull case _ => InternalServerError("Cannot access to the db") //error on write on the db } } } case (_, _) => Future{BadRequest("json bad formed")} //the json is bad formed } case None => Future{BadRequest("Need a json value")} //need a json value } }
하지만 컴파일러는 나에게 다음과 같은 오류가 있습니다 :
나는 다음과 같은 방법을 구현 한
[error] /Users/alberto/git/bdrim/modules/recommendation-system/app/recommendationsystem/controllers/Application.scala:176: type mismatch;
[error] found : scala.concurrent.Future[play.api.mvc.Result]
[error] required: play.api.mvc.Result
[error] Users.save(user).map{error => error match {
[error] ^
[error] /Users/alberto/git/bdrim/modules/recommendation-system/app/recommendationsystem/controllers/Application.scala:176: type mismatch;
[error] found : scala.concurrent.Future[play.api.mvc.Result]
[error] required: play.api.mvc.Result
[error] Users.save(user).map{error => error match {
[error] ^
[error] one error found
문제점은 무엇을?
감사하지 작동하는지 확인합니다. –