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

211
Visualizações
Row results from Tedious Request.on not being saved

I have a function queryDatabase() in my nodeJS application, which is responsible for querying an Azure SQL DB and returning the result, using the Tedious library to connect to the DB.

Currently, to return the data retrieved from the DB I am iterating through the rows returned by the query, saving the data to a var (an array of dicts, essentially JSON really), and returning it. However I'm experiencing an issue where the var is returned (empty) before anything can be assigned to it.

I'm pretty sure this would have something to do with the callback functions in request.on() executing after the return is called, but I am a bit inexperienced with this side of JS and haven't had much luck with online documentation/tutorials.

function queryDatabase(){


 console.log("Reading rows from the Table...");

  // Read all rows from table
  const request = new Request(
    `select TOP 5 level, city, address from [dbo].[mainTable];`,
    (err, rowCount) => {
      if (err) {
        console.error(err.message);
      }
    }
  );

  var result = [];

  request.on("row", (columns) => {
    var entry = {};
    columns.forEach((column) => {
      entry[column.metadata.colName] = column.value;
    });
    result.push(entry);
  });

  connection.execSql(request);

  return result;
}

Any help would be greatly appreciated, cheers!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Request is an event emitter, so its all about waiting for all the events you care about to happen, then returning the result. This is what Promise's are for. A promise is an async action that will resolve at some future date. you need to wait for all row events to be emitted, collect them, then wait for the done event, and resolve the promise. I have modified your code to achieve this, and shown example for usage.

function queryDatabase(){

  console.log("Reading rows from the Table...");

  // Read all rows from table
  const request = new Request(
    `select TOP 5 level, city, address from [dbo].[mainTable];`,
    (err, rowCount) => {
      if (err) {
        console.error(err.message);
      }
    }
  );

  return new Promise((resolve,reject)=>{
    const result = [];
  
    request.on("row", (columns) => {
      const entry = {};
      columns.forEach((column) => {
        entry[column.metadata.colName] = column.value;
      });
      result.push(entry);
    });

    request.on('error',error=>reject(error));// some error happened, reject the promise
    request.on('done',()=>resolve(result)); // resolve the promise with the result rows.

    connection.execSql(request);

  });

}

Usage shown below:

queryDatabase().then(rows=>{
  // do something with the rows
})
.catch(error=>{
  // do something with the error
})
about 4 years ago · Juan Pablo Isaza Relatório

0

@r3wt answer is correct but this one worked for me:

 request.on("doneProc", () => resolve(result)); 

Instead of

request.on('done',()=>resolve(result));
about 4 years ago · Juan Pablo Isaza 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