Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda