0

I'm using mssql(Microsoft SQL Server client for Node.js) package from npm.I'm trying to execute a stored procedure residing in my sql server database.Everything works fine.However what I want to do is return the recordsets so that i can export this to be used in other module.Below is what I'm trying to do.

function monthlyIceCreamSalesReport (scope){
var connObj = connConfig();
connObj.conn.connect(function(err){
    if(err){
        console.log(err);
        return;
    }
    connObj.req.input('Month',4);
    connObj.req.input('Year',2016);

    connObj.req.execute('<myStoredProcedure>', function(err, recordsets, returnValue){
        if(err){
            console.log(err);
        }
        else {
            console.log(recordsets[0]); // successfully receiving the value
        }
        connObj.conn.close();
    });
  });
  console.log('check for recordsets', recordsets[0]); // undefined
  return recordsets[0];
}
var sqlServerObj = {
    monICSalesReport : monthlyIceCreamSalesReport,
};
module.exports = sqlServerObj;

As shown in the code snippet, since the value of recordsets[0] is undefined, exporting this function is of no use.

bikash
  • 433
  • 1
  • 6
  • 18
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Ben Fortune Sep 23 '16 at 12:16

2 Answers2

0

You can't return this way in async nature. You can get it by passing the callback function

Try to give a callback function like this

function monthlyIceCreamSalesReport(scope, callback) { // pass a callback to get value

    var connObj = connConfig();
    connObj.conn.connect(function(err) {
        if (err) {
            console.log(err);
            return;
        }
        connObj.req.input('Month', 4);
        connObj.req.input('Year', 2016);

        connObj.req.execute('<myStoredProcedure>', function(err, recordsets, returnValue) {
            if (err) {
                console.log(err);
            } else {
                console.log(recordsets[0]);
                connObj.conn.close();
                return callback(null, recordsets[0]); //return as a callback here and get that value in callback from where you called this function 
            }
        });
    });
 }
var sqlServerObj = {
    monICSalesReport: monthlyIceCreamSalesReport,
};
module.exports = sqlServerObj;

Note: See the comment to understand the changes

abdulbarik
  • 6,101
  • 5
  • 38
  • 59
-1

recordsets[0] is undefinded, because is defined only in connObj.req.execute function scope. You may do this in this way:

function monthlyIceCreamSalesReport (scope, cb){
 var connObj = connConfig();
 connObj.conn.connect(function(err){
   if(err){
      console.log(err);
      return cb(Error("Something wrong"));
  }
  connObj.req.input('Month',4);
  connObj.req.input('Year',2016);

connObj.req.execute('<myStoredProcedure>', function(err, recordsets, returnValue){
    if(err){
        console.log(err);
        connObj.conn.close();
        return cb(Error("Something wrong"));
    }
    else {
        console.log(recordsets[0]); // successfully receiving the value

        connObj.conn.close();
        return cb(recordsets[0]);
    }
  });
});

}

var sqlServerObj = {
   monICSalesReport : monthlyIceCreamSalesReport,
};

module.exports = sqlServerObj;
SkyQ
  • 380
  • 2
  • 9