Search code examples
promisenestjstypeorm

Promise.all return empty array


I am tring return the modified fetched data.But when i return the modified array it is empty. But when i return a particular index of the array there is value returned. I am using nestjs with typeorm below i will attach my code:

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   

I am not that much used promise.Thanks in advance.


Solution

  • In the data.forEach((value,index) function, you are changing the return_array values at every iteration, so only the last one will set the values.

    Moreover, you are using .then in an async function, so the code is more difficult to read

    This should work:

    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
    }