简体   繁体   中英

Reading data from a sql server stored procedure in Node js?

I am trying to get data returned from a sql server stored procedure and then fetch individual data by passing column names. For some reason, I am getting undefined when I pass column names. This is what I tried so far, what could I be doing wrong?

There is an issue with the signature of execute callback. It should be function(result, returnValue) where result will be recordSet

var updateMember = function( Country, BankID) {
   new sql.Request()
    .input('Country', sql.VarChar(50), Country)
    .input('BankID', sql.VarChar(50),  BankID)
    .execute('p_GetTransactionsStats').then(function(result, returnValue) {
      console.dir(result); // it should be [{},{}]

      for (var i = 0; i < result.length; i++) {
        console.log("Merchantname :-" + result[i].MERCHANTNAME);
        console.log("Bankname :-" + result[i].BankName);
      }
  }).catch(function(err) {
    console.dir(err);
  });
};

Second Solution

Without changing the promise callback method, you can just tweak the way how you get value from result.

var updateMember = function( Country, BankID) {
   new sql.Request()
    .input('Country', sql.VarChar(50), Country)
    .input('BankID', sql.VarChar(50),  BankID)
    .execute('p_GetTransactionsStats').then(function(result) {
      console.dir(result); // it should be [[{},{}], returnValue:0]
      var records = result[0];

      for (var i = 0; i < records.length; i++) {
        console.log("Merchantname :-" + records[i].MERCHANTNAME);
        console.log("Bankname :-" + records[i].BankName);
      }
  }).catch(function(err) {
    console.dir(err);
  });
};

Hope it helps you.

Your results with is an array or array Hence you need to access values as:

    for (var i = 0; i < result.length; i++) {
        console.log("Merchantname :-" + result[0][i].MERCHANTNAME);
        console.log("Bankname :-" + result[0][i].BankName);
    }

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