Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 使用reactivemongo的异步播放方法出错_Scala_Asynchronous_Playframework_Future_Reactivemongo - Fatal编程技术网

Scala 使用reactivemongo的异步播放方法出错

Scala 使用reactivemongo的异步播放方法出错,scala,asynchronous,playframework,future,reactivemongo,Scala,Asynchronous,Playframework,Future,Reactivemongo,我正在使用带有Scala的Reactivemongo编写一个Play 2.3.2应用程序。 在我的数据库中,我将所有用户数据存储在一个集合中 现在,我正在编写一个具有以下行为的异步方法: 该方法从request.body对象获取json值 如果值的格式不正确,该方法将返回错误的请求响应 如果集合已经包含一个表示同一用户对象的文档,则该方法返回Ok响应 否则,方法将在db上插入文档并返回Ok响应 我已经实现了如下方法: def addUser = CorsAction.async { reques

我正在使用带有Scala的Reactivemongo编写一个Play 2.3.2应用程序。 在我的数据库中,我将所有用户数据存储在一个集合中

现在,我正在编写一个具有以下行为的异步方法:

  • 该方法从request.body对象获取json值
  • 如果值的格式不正确,该方法将返回错误的请求响应

  • 如果集合已经包含一个表示同一用户对象的文档,则该方法返回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
    

    有什么不对???

    当您达到代码复杂度级别时,您不知道发生了什么,最好的选择几乎总是将问题分成更小的部分。一个好主意可能是在您不确定的地方显式输入,编译器会告诉您哪里出错了


    这样做,您可能会很快发现类型不匹配的地方。

    如果查看编译器错误消息,返回类型是错误的

    那是因为你有:

    Users.find(...)....map {
        ....
        ....
        Users.save(user).map {
            ....
        }
    }
    
    返回一个Future[Future[Result]]而不是Future[Result]。 要解决这个问题,您需要使未来变得平坦

    尝试:

    Users.find(...)....flatMap {
        ....
        ....
        Users.save(user).map {
            ....
        }
    }