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