scala playframework在flatMap中返回Ok

时间:2017-08-07 09:45:00

标签: scala playframework

我有以下功能:

def createProcessTemplate: Action[AnyContent] = Action.async { implicit request =>
    println(request.body.asJson)
    request.body.asJson.map(_.validate[ProcessTemplatesModel] match {
      case JsSuccess(process, _) =>
        processTemplateDTO.createProcessTemplate(process).map {
          process => {

            // Create process steps templates
            createNewProcessStepTemplate(request, process.id.get)


            // Create prerequisites
            createProcessTemplatePrerequisite(request, process.id.get)

            // Create postConditions
            createProcessTemplatePostCondition(request, process.id.get)

            processTemplateDTO.getProcessStepTemplates(process.id.get).flatMap(
              steps => Ok(Json.obj("id" -> process.id, "title" -> process.title, "steps" -> steps)))
          }
        }
      case JsError(_) =>
        Future.successful(BadRequest("Process Template not create client"))
      case _ => Future.successful(BadRequest("Process Template create client"))
    }).getOrElse(Future.successful(BadRequest("Process Template create client")))
  }

我想返回Ok语句中的步骤,但是出现了这个错误:

enter image description here

在这种情况下会出现什么问题?

感谢

1 个答案:

答案 0 :(得分:2)

试试这段代码:

def createProcessTemplate: Action[AnyContent] = Action.async { implicit request =>
println(request.body.asJson)
request.body.asJson.map(_.validate[ProcessTemplatesModel] match {
  case JsSuccess(process, _) =>
    processTemplateDTO.createProcessTemplate(process).flatMap {
      process => {

        // Create process steps templates
        createNewProcessStepTemplate(request, process.id.get)


        // Create prerequisites
        createProcessTemplatePrerequisite(request, process.id.get)

        // Create postConditions
        createProcessTemplatePostCondition(request, process.id.get)

        processTemplateDTO.getProcessStepTemplates(process.id.get).map(
          steps => Ok(Json.obj("id" -> process.id, "title" -> process.title, "steps" -> steps)))
      }
    }
  case JsError(_) =>
    Future.successful(BadRequest("Process Template not create client"))
  case _ => Future.successful(BadRequest("Process Template create client"))
}).getOrElse(Future.successful(BadRequest("Process Template create client")))

}

我已经切换了map方法调用周围出现的flatMapprocessTemplateDTO。在第一个中,您需要flatMap,因为您返回Future,但在第二个中您实际需要map,因为您返回Result