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

150
Views
Array of Object not displaying content after .push()

I am trying to push object inside array within forEach iteration.

const dashboard_get = async (req, res) => {
    let records = await Record.find();
    let visitors = await Visitor.find();

    let log = {};
    let logs = [];

    records.forEach((record) => {
        visitors.forEach(async (visitor) => {
            if(record.visitor_id === visitor._id.toString()) {
                let establishment = await Establishment.findById({_id:record.establishment_id});
                
                log.id = record.visitor_id;
                log.name = `${visitor.name.fname} ${visitor.name.mi} ${visitor.name.lname}`;
                log.establishment = `${establishment.name}`;
                log.address = `${establishment.address}`;
                log.date = record.createdAt;
            
                logs.push(log);
                //displaying here
                console.log(logs); 
                log = {};
            }
        })  
    })

    // Not displaying here
    console.log(logs); 
    res.render('./Administrator Module/dashboard', {logs});
}

Inside the nested forEach, the logs are displaying correctly. However, I can't access the logs after the forEach. Is pushing of object inside array within forEach will not retain the data?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The issue is, the promise returned by the iteration function is ignored by forEach(). check that Using async/await with a forEach loop

about 4 years ago · Juan Pablo Isaza Report

0

In your code the problem is the declaration of log object outside the for loop.It is all about call by reference and call by value issue. if you move log object inside if condition in nested loop than it will work fine. Do it like this

const dashboard_get = async (req, res) => {
let records = await Record.find();
let visitors = await Visitor.find();
let logs = [];
records.forEach((record) => {
    visitors.forEach(async (visitor) => {
        if (record.visitor_id === visitor._id) {
            let establishment = await Establishment.findById({ _id: record.establishment_id });
            const log = {
                id: record.visitor_id, 
                name: `${visitor.name.fname} ${visitor.name.mi} ${visitor.name.lname}`,
                establishment: establishment.name, 
                address: establishment.address, 
                date: record.createdAt
            };
           
            logs.push(log);
            //displaying here
            console.log(logs);
        }
    })
});

// Not displaying here
console.log(logs);
res.render('./Administrator Module/dashboard', { logs });
}
about 4 years ago · Juan Pablo Isaza Report
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!