I am attempting to set up an API that pulls from an Azure DB.
I have a function queryDatabase() that sends a select query to an Azure SQL Database. It sends the query request and then returns a Promise waiting on the response from the database:
function queryDatabase(){
const request = new Request(
`select * from [dbo].[table1];`,
(err, rowCount) => {
if (err) {
console.error(err.message);
}
}
);
return new Promise((resolve,reject)=>{
const result = [];
request.on("row", (columns) => {
const entry = {};
columns.forEach((column) => {
entry[column.metadata.colName] = column.value;
});
result.push(entry);
});
request.on('done',()=>resolve(result)); // resolve the promise with the result rows.
request.on('error',error=>reject(error));// some error happened, reject the promise
connection.execSql(request);
});
}
Then for usage I have a router listening on /data which is supposed to call the queryDatabase() method and wait for the Promise to be fulfilled. Once it’s fulfilled it should respond with the data in a JSON format, however the Promise from queryDatabase() never seems to be fulfilled, nor errored, as the page is stuck on loading:
router.get("/data", cors(), function (req, res) {
queryDatabase().then(rows=>{
res.type('application/json');
res.send(JSON.stringify(rows));
})
.catch(error=>{
res.status(500);
res.render('error, ', {error: error});
});
});
Any help on why it’s not working correctly would be amazing, cheers