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

169
Views
Variable not being changed inside callback
let dbResult

//insert marker into db
await pool.query(
    'INSERT INTO markers (title, description, creator, active) VALUES ($1, $2, $3, $4) RETURNING *',
    [title, description, creator, active],
    (error, results) => {
        if (error) {
            throw error
        }
        dbResult = results;
    });

console.log(dbResult)

Not really sure why but the dbResult variable is not being changed. I'm not very experienced with JavaScript, can anyone explain why this is happening.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You have to log it when it is changed:

let dbResult

//insert marker into db
await pool.query(
    'INSERT INTO markers (title, description, creator, active) VALUES ($1, $2, $3, $4) RETURNING *',
    [title, description, creator, active],
    (error, results) => {
        if (error) {
            throw error
        }
        dbResult = results;
        console.log(dbResult)
    });

This is because the callback function (error, results) => ... gets triggered after the line console.log(dbResult) and so you are printing out the outdated value.

You might want to do some research on callback functions and async/await in JavaScript to get a better understanding.

about 4 years ago · Santiago Trujillo 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!