简体   繁体   中英

returning data from node mssql execute functions

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.

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM