1
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.

Eduardo
  • 29
  • 3

1 Answers1

1

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.

David Callanan
  • 5,601
  • 7
  • 63
  • 105