I'm trying to create an Azure function which connects to a SQL Server. The response is not getting executed. I can see the correct data returned in the log. Any ideas on how I can get the context.res to return after the request is completed?
var rows = [];
module.exports = function (context, req, res) {
context.log('JavaScript HTTP trigger function processed a request.');
if(req.query.nfcID) {
connection.on('connect', function(err) {
request = new Request("select tagUID, ProductID, Active From Tags where TagUID = "+ req.query.nfcID,
function(err, rowCount, rows) {
if (err) {
context.log(err);
} else {
connection.close();
context.log('rows: '+rowCount);
}
});
request.on('row', function(columns) {
var row = {};
columns.forEach(function(column) {
row[column.metadata.colName] = column.value;
});
rows.push(row);
context.log(rows);
context.res = {
body: rows;
}
});
connection.execSql(request);
});
}
else {
context.res = {
status: 400,
body: "Please provide the nfc id"
};
}
context.done();
};
Please change your code to:
var rows = [];
module.exports = function(context, req, res) {
context.log('JavaScript HTTP trigger function processed a request.');
if (req.query.nfcID) {
connection.on('connect', function(err) {
request = new Request("select tagUID, ProductID, Active From Tags where TagUID = " + req.query.nfcID, function(err, rowCount, rows) {
if (err) {
context.log(err);
} else {
connection.close();
context.log('rows: ' + rowCount);
}
});
request.on('row', function(columns) {
var row = {};
columns.forEach(function(column) {
row[column.metadata.colName] = column.value;
});
rows.push(row);
context.log(rows);
context.res = {
body: JSON.stringify(rows);
}
context.done();
});
connection.execSql(request);
});
} else {
context.res = {
status: 400,
body: "Please provide the nfc id"
};
context.done();
}
};