promise - 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
我没有太多用过的诺言。在此先感谢。
解决方案
在函数中,您在每次迭代时都会data.forEach((value,index)
更改值,因此只有最后一个会设置值。return_array
此外,您.then
在async
函数中使用,因此代码更难阅读
这应该有效:
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
}
推荐阅读
- assembly - 将 AH 存储到 DW 变量中时“操作数类型不匹配”
- nginx - 如何将 auth_request 发送到 nginx 中的变量 URI?
- asp.net - 将 asp.net 核心应用程序发布到 azure 时出错
- php - 如何使用孩子创建 json 数组 php
- cloud - activiti-7 setup *** 没有规则来制作目标 `activiti-cloud-audit'
- angular - Angular 可观察到的错误不会使用 ChangeDetectionStrategy.OnPush 刷新界面
- python - 如何将 utf-8 编码转换为字符串?
- regex - 使用正则表达式选择数据并清理它以输出
- typescript - 如何从数组中获得类似枚举的行为?打字稿
- c# - 更改“TextBox.ScrollBars”时“创建窗口句柄时出错”