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

119
Views
variable changes inside forEach not saving

I have these lines here

let users = []
members.forEach(member => {
  let profile

  db.get(member.id).then(value => {
    profile = JSON.parse(value)
  })

  if(profile == null)
    return

  if(profile.bank + profile.cash > 0)
  {
    let user = new User(member, profile)
    users.push(user)
  }
})
console.log(users)

I want to save User objects into the users array, but outside the forEach loop, it is undefined. Can anyone help me?

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

0

You problem is how javascript works --

 (member => 

represents a function call. But that function may not be finished by the time you get to your console log. You need a promise to make sure it is done.

Best way to wait for .forEach() to complete

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!