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

1.2K
Visualizações
Node.JS MySQL waiting for query results

I want to make a function that query data to MySQL, and wait for the result.

const results = await message.client.db.query("SELECT * FROM TABLE WHERE condition");
console.log(results);   

My issue is that results doesn't contain the raw data as I would expect it, but a query object :

<ref *1> Query {
  _events: [Object: null prototype] {
    error: [Function (anonymous)],
    packet: [Function (anonymous)],
    timeout: [Function (anonymous)],
    end: [Function (anonymous)]
  },
  _eventsCount: 4,
  _maxListeners: undefined,
  _callback: [Function (anonymous)],
...
    _connectCalled: true,
    state: 'authenticated',
    threadId: 27,
    [Symbol(kCapture)]: false
  },
  [Symbol(kCapture)]: false
}

I have been looking for a doc on that Object, and I couldn't find it. I suppose it is a sort of Promise, as the Query object is returned before the results are received :

console.log("1 - Before Query");
const results = await message.client.db.query("SELECT * FROM TABLE WHERE condition", async function (err, result, fields) {
    if (err) throw err;
    console.log("2 - Query result : ");
    console.log(result);
});
console.log("3 - After Query");
console.log(results);   

OUTPUT : 
1 - Before Query
3 - After Query
<The Query Object>
2 - Query result : 
<An array with my data>

I have seen a lot of example where people use await connection.query("...") to wait for the results, and I don't understand where does this Query object comes from.

I know I can wait for the result with something like that :

    message.client.db.query("SELECT * FROM TABLE WHERE condition", async function (err, result, fields) {
        if (err) throw err;
        do_something(result);
    });

But I first find it really heavy, and hard to use when you do a lot of requests. Also I need an async function that return a value from database, and since await connection.query("...") can't wait for the result, I don't know how to do it.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If you are using mysql2 package to connect to the database you can do something like this:

await con1.promise().query("your query here")
    .then( ([rows,fields]) => {
        your var = rows[0]['field1'];
        return nome_completo;
    })
    .catch(console.log)

Where con1 is the connection you defined. With promise() you are promisifying your query so you can use await

over 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