I have debuged through the code to check to make sure that I am getting data back from my sql connection within node.js. But I cannot get this function to return a the results. When looping through the rows it is adding the item to the array but which i commented the line where the value appears correct...Why is this function returning a blank array []?
function execute_stp_getconfigurationsbyuserid(userid, callback)
{
var results = [];
var Connection = require('tedious').Connection;
var config = {
};
function item(id,name)
{
this.id = id;
this.name = name;
return this;
}
var Request = require('tedious').Request
var TYPES = require('tedious').TYPES;
var connection = new Connection(config);
var myCallback = function(err,data){
connection.on('connect', function(err) {
var request = new Request("stp_GetConfigurationsByUserId @UserId", function(err) {
if (err) {
console.log(err);}
});
request.addParameter('UserId', TYPES.Int, userid);
request.on('row', function(columns) {
var itm = new item(columns[0].value, columns[2].value);
results.push(itm);
//HAS THE CORRECT VALUE HERE
console.log(results);
});
request.on('done', function(rowCount, more) {
console.log(rowCount + ' rows returned');
myCallback(null,results);
});
connection.execSql(request);
});
}
}
Calling function by:
router.get('/getuserconfigurations', function(req, res, next) {
var config_ddl = execute_stp_getconfigurationsbyuserid(1);
console.log("Getting user configurations....")
console.log(config_ddl);
res.send(JSON.stringify(config_ddl));
});