I'm using mssql with express.js. I'm trying to set the recordset results into a dictionary and then returning that dictionary using res.send but dictionary is empty. What am I doing wrong?
Here's what I have tried:
app.post('/api/v1/test', async (req, res) => {
tableNames = ["test", "test2"]
var response = {};
for (let index = 0; index < tableNames .length; index++) {
const table = tableNames [index];
let query = `SELECT * FROM ${table }`
await queryDatabase(query , function(err, result){
response[table ] = result.recordset;
});
}
res.send(
{
statusCode: 200,
data: response
}
);
});
function queryDatabase(query, callback) {
sql.connect(dbConfig, function (err) {
if (err) {
console.log(err);
}
var request = new sql.Request();
request.query(query, function (err, result) {
if (err) throw err
callback(null, result)
});
});
}
queryDatabase()does not return a promise and thusawaitdoes nothing useful with it so yourforloop does not pause waiting for the query to complete and thus you callres.send()before yourresponseobject contains the data.You can either switch to a sql module that directly supports promises or you can modify
queryDatabase()to support promises and then modify how it's called to use the returned promise like this: