Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

88
Views
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 answers
Answer question

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 Report

0

I solved it by adding this:

    request.on('requestCompleted', () => {
        res.send(result)
    })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!