Play框架Scala项目:向Seq添加int并返回它

时间:2017-08-31 13:43:11

标签: scala playframework

如何迭代Future[Seq[]]并将Int值添加到Seq并返回此Seq

我的代码如下所示:

def createCopyOfProcessTemplate(processTemplateId: Int): Action[AnyContent] = Action.async {
processTemplateDTO.createCopyOfProcessTemplate(processTemplateId).map { process =>
  processTemplateDTO.getProcessStepTemplates(processTemplateId).map { steps =>
    steps.foreach(processStep =>
      processTemplateDTO.createCopyOfStepTemplates(processTemplateId, process.get.id.get, processStep))
  }

  // Fetch all Steps which belongs to this Template
  val processSteps: Future[Seq[ProcessStepTemplatesModel]] = processTemplateDTO.getProcessStepTemplates(processTemplateId)

  val stepIds:Seq[Int]=Seq()

  processSteps.map(
    steps => steps.foreach(
      step => stepIds:+step.id
    )
  )

  Ok(Json.obj("process" -> process, "steps" -> stepIds))
}
}

由于异步性,我认为Seq是空的。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

你可以采取不同的方法:

Action.async {
  ...
  processSteps.map(_.map(_.id))
              .map(ids => Ok(Json.obj("process" -> process, "steps" -> ids)))
}