【发布时间】:2014-10-13 13:49:39
【问题描述】:
我正在使用带有 Scala 的 Reactivemongo 编写一个 Play 2.3.2 应用程序。 在我的数据库中,我将所有用户数据存储在一个集合中。
现在我正在编写具有以下行为的异步方法:
- 该方法从 request.body 对象中获取 json 值
如果值不正确,则该方法返回错误的请求响应。
如果集合中已经包含一个表示相同用户对象的文档,则该方法返回一个 Ok 响应。
- 否则该方法将文档插入数据库并返回 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
怎么了??
【问题讨论】:
标签: scala asynchronous playframework future reactivemongo