I have a function queryDatabase() in my nodeJS application, which is responsible for querying an Azure SQL DB and returning the result, using the Tedious library to connect to the DB.
Currently, to return the data retrieved from the DB I am iterating through the rows returned by the query, saving the data to a var (an array of dicts, essentially JSON really), and returning it. However I'm experiencing an issue where the var is returned (empty) before anything can be assigned to it.
I'm pretty sure this would have something to do with the callback functions in request.on() executing after the return is called, but I am a bit inexperienced with this side of JS and haven't had much luck with online documentation/tutorials.
function queryDatabase(){
console.log("Reading rows from the Table...");
// Read all rows from table
const request = new Request(
`select TOP 5 level, city, address from [dbo].[mainTable];`,
(err, rowCount) => {
if (err) {
console.error(err.message);
}
}
);
var result = [];
request.on("row", (columns) => {
var entry = {};
columns.forEach((column) => {
entry[column.metadata.colName] = column.value;
});
result.push(entry);
});
connection.execSql(request);
return result;
}
Any help would be greatly appreciated, cheers!