Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
Unable to populate object inside forEach containing async Javascript

This fn is a part of a middleware that intends to populate array of PROVIDERS with an array of ENCOUNTERS.

When I console.log(provider['ENCOUNTERS']) then I do get the encounters : console.log(provider['ENCOUNTERS']) But when I console.log(provider) in the same place, I don't get encounters : console.log(provider)

I think that's why the commented console.log(network.PROVIDERS) doesn't contain ENCOUNTERS.


            populateEncounters = async()=>{
                var promises = [];

                network.PROVIDERS.forEach((provider)=>{
                    promises.push(
                        (async (provider)=>{
                            const e = await Encounter.find({
                                PROVIDER:provider._id
                            })
                            return e;
                        })(provider)
                        .then((e)=>{
                            provider['ENCOUNTERS'] = e;
                            console.log(provider['ENCOUNTERS']);
                        })
                        .catch ((error) => {
                            console.log('Error: ', error);
                        })
                    );
                });
                    
                Promise.all(promises).then(() => {
                    // console.log(network.PROVIDERS)
                    res.network = network;
                    next()
                });
            }

            await populateEncounters();
            
about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!