0

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));
});
user3788671
  • 1,977
  • 5
  • 29
  • 43

1 Answers1

1

Node works asynchronously.By the time return results is executed, above statements need not be completed. Try using callbacks to pass/act on data. Refer this link nodeJs callbacks simple example

Edit: The code can be modified to

var callback = function(err, data){
  console.log(data);
}

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

 
  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');
      callback(null,results);
    });

    connection.execSql(request);

  });
   

}
Community
  • 1
  • 1