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

107
Visualizações
Returning resolved promise result through Express router

I am attempting to set up an API that pulls from an Azure DB using the tedious library.

I have a function queryDatabase() that sends a select query to an Azure SQL Database. It sends the query request and then returns a Promise waiting on the response from the database:

 function queryDatabase(){
  const request = new Request(
    `select * from [dbo].[table1];`,
    (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('done',()=>resolve(result)); // resolve the promise with the result rows.
    request.on('error',error=>reject(error));// some error happened, reject the promise

    connection.execSql(request);
  });
}

Then for usage I have a router listening on /data which is supposed to call the queryDatabase() method and wait for the Promise to be fulfilled. Once it's fulfilled it should respond with the data in a JSON format, however the Promise from queryDatabase() never seems to be fulfilled, nor errored, as the page is stuck on loading:

  router.get("/data", cors(), function (req, res) {
  queryDatabase().then(rows=>{
    res.type('application/json');
    res.send(JSON.stringify(rows));
  })
  .catch(error=>{
    res.status(500);
    res.render('error, ', {error: error});
  });
});

Any help on why it's not working correctly would be amazing, cheers

about 4 years ago · Juan Pablo Isaza
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