Promise.all return 空数组
Promise.all return empty array
我正在尝试 return 修改后的 data.But 当我 return 修改后的数组是空的。但是当我 return 数组的特定索引时,有值 returned。我在下面使用带有 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
我提前没怎么用promise.Thanks
在 data.forEach((value,index)
函数中,您要在每次迭代中更改 return_array
值,因此只有最后一次会设置值。
此外,您在 async
函数中使用 .then
,因此代码更难阅读
这应该有效:
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
}
我正在尝试 return 修改后的 data.But 当我 return 修改后的数组是空的。但是当我 return 数组的特定索引时,有值 returned。我在下面使用带有 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
我提前没怎么用promise.Thanks
在 data.forEach((value,index)
函数中,您要在每次迭代中更改 return_array
值,因此只有最后一次会设置值。
此外,您在 async
函数中使用 .then
,因此代码更难阅读
这应该有效:
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
}