• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

166
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();
            
almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error