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

147
Views
MySql2 query function scope not updating variable outside of the function
var names = [];

db.query(`SELECT * FROM department`, (err, rows) => {
      if (err) {
         console.log(err);
         return;
      }
      names = rows.map(x => x.name);
})

console.log(names);

Hi guys, been messing with this code all day but can't for the life of me figure out why my variable "names" will always print an empty array, rather than be populated with what occurs during this query. I defined it outside of the query scope, so shouldn't that mean that the value I populate it with inside the scope should persist afterwards?

about 4 years ago · Juan Pablo Isaza
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!