首页 > 解决方案 > Promise.all 返回空数组

问题描述

我正在返回修改后的获取数据。但是当我返回修改后的数组时,它是空的。但是当我返回数组的特定索引时,就会返回值。我在下面使用带有typeorm的nestjs,我将附上我的代码:

let patientData = await this.patientRepository.find({where:{
  hospitalId:option.hospitalId
}})
if(patientData.length){
  let return_data:any = []
  await Promise.all(patientData.map(async patient => {
    return this.patientProcedureValueRepository
        .createQueryBuilder("patient_procedure_value")
        .leftJoinAndSelect("patient_procedure_value.patientProcedure","patient_procedures")
        .where("patient_procedure_value.patient_id = :patientId",{patientId:patient.patientId})
        .andWhere("patient_procedure_value.hospital_id = :hospitalId",{hospitalId:patient.hospitalId})
        .andWhere("patient_procedures.procedure_name IN(:...ids)",{ids:["Patient Name: First","Patient Name: Middle","Patient Name: Last","Patient Date of Birth"]})
        .getRawMany()   
    }))
    .then((data) => {
      let return_array:any = []     
      let tmp_data1 = data.forEach((value,index) => {
        let first = value.filter((procedure) => (procedure.patient_procedures_procedure_name === "Patient Name: First"))
        let middle = value.filter((procedure) => (procedure.patient_procedures_procedure_name === "Patient Name: Middle"))
        let last = value.filter((procedure) => (procedure.patient_procedures_procedure_name === "Patient Name: Last"))
        let dob = value.filter((procedure) => (procedure.patient_procedures_procedure_name === "Patient Date of Birth"))
        return_array.patientId = (first.length?first[0].patient_procedure_value_patient_id:"")
        return_array.firstName = (first.length?first[0].patient_procedure_value_patient_procedure_value:"")
        return_array.middleName = (middle.length?middle[0].patient_procedure_value_patient_procedure_value:"")
        return_array.lastName = (last.length?last[0].patient_procedure_value_patient_procedure_value:"")
        return_array.birthdate = (dob.length?dob[0].patient_procedure_value_patient_procedure_value:"")
        if(return_array.patientId !== ""){
          return_data.push(return_array)
        }            
      })              
    })
  return return_data
  //Below return perticular patient Id correctly
  //return return_data[0].patientId   

我没有太多用过的诺言。在此先感谢。

标签: promisenestjstypeorm

解决方案


在函数中,您在每次迭代时都会data.forEach((value,index)更改值,因此只有最后一个会设置值。return_array

此外,您.thenasync函数中使用,因此代码更难阅读

这应该有效:

if (patientData.length) {
  const data = await Promise.all(patientData.map(patient => {
    return this.patientProcedureValueRepository
      .createQueryBuilder("patient_procedure_value")
      .leftJoinAndSelect("patient_procedure_value.patientProcedure", "patient_procedures")
      .where("patient_procedure_value.patient_id = :patientId", { patientId: patient.patientId })
      .andWhere("patient_procedure_value.hospital_id = :hospitalId", { hospitalId: patient.hospitalId })
      .andWhere("patient_procedures.procedure_name IN(:...ids)", { ids: ["Patient Name: First", "Patient Name: Middle", "Patient Name: Last", "Patient Date of Birth"] })
      .getRawMany()
  }))


  const return_array = data.map((value) => {
    const first = value.filter((procedure) => (procedure.patient_procedures_procedure_name === "Patient Name: First"))
    const middle = value.filter((procedure) => (procedure.patient_procedures_procedure_name === "Patient Name: Middle"))
    const last = value.filter((procedure) => (procedure.patient_procedures_procedure_name === "Patient Name: Last"))
    const dob = value.filter((procedure) => (procedure.patient_procedures_procedure_name === "Patient Date of Birth"))
    const obj = {
      patientId: (first.length ? first[0].patient_procedure_value_patient_id : ""),
      firstName: (first.length ? first[0].patient_procedure_value_patient_procedure_value : ""),
      middleName: (middle.length ? middle[0].patient_procedure_value_patient_procedure_value : ""),
      lastName: (last.length ? last[0].patient_procedure_value_patient_procedure_value : ""),
      birthdate: (dob.length ? dob[0].patient_procedure_value_patient_procedure_value : ""),
    }
    if (obj.patientId !== "") {
      return obj
    }
    return null
  })
    .filter(obj => obj !== null) // remove nulls
}

推荐阅读