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

90
Visualizações
filled array is empty when send back to user

I try to get data from an azure database with SQL and for this I use tedious. When I execute the code, the array "result" gets filled up with data but when de respond sends the array "result" to the user it is empty. Why and how can I solve it? (I want to send it back in JSON format, the legth is unknown I use 5 to debug).

This is my code

router.get('/', async (req, res) => {
    result = []
    let rowData = {}
    const request = new Request(
        `SELECT TOP (5) [LogID]
        ,[OpdrachtID]
        ,[ChangeLog]
        ,[TimeStamp]
        ,[PersonID]
    FROM [log].[LOpdracht]`,
    function (err, rowCount, rows) {
        if (err) throw err
    })
    connection.execSql(request)

    request.on('row', function(columns){
        rowData = {}
        columns.forEach(function(column){
            rowData[column.metadata.colName] = column.value
        })
        result.push(rowData)
    })

    res.send(result)
})

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

0

Looks like you are using Tedious. If that is so then you can use the 'done' event to be notified when the request has completed and all rows have been read into result.

request.on('done', () => res.send(result));

Update: As the note in the documentation points out, since you are using execSql you will need to listen to doneProc and doneInProc instead:

request.on('doneProc', () => res.send(result));
request.on('doneInProc', () => res.send(result));
about 4 years ago · Juan Pablo Isaza Relatório

0

I solved it by adding this:

    request.on('requestCompleted', () => {
        res.send(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