javascriptnode.jsjsontedious

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)
})


Solution

  • I solved it by adding this:

        request.on('requestCompleted', () => {
            res.send(result)
        })