1

I am trying to get the return value from a js function in node js and return the value in a post method in my server file. But for some reason, I keep getting undefined. What could I be doing wrong ?My goal is to return the records variable to my server.js file This is what I tried so far:-

sqlconfig.js

var sql = require('mssql');
var config = {
  user: 'realm',
  password: 'friend',
  server: '123.45.67.89\\SQL2014',
  database: 'ArtCaffee'
};

module.exports = {
  sqlconnecttodb: function() {
    sql.connect(config, function(err) {
      if (err) console.log("message is for" + err);
    });
  },

  updateMember: function(Country, BankName, Password, PublicIP, UserID) {
    sql.connect(config, function(err) {
      if (err) console.log("message is for" + err);
      new sql.Request()
        .input('Country', sql.VarChar(50), Country)
        .input('BankName', sql.VarChar(50), BankName)
        .input('Password', sql.VarChar(50), Password)
        .input('PublicIP', sql.VarChar(50), PublicIP)
        .input('UserID', sql.VarChar(50), UserID)
        .execute('p_LoginUser').then(function(result) {
          var records = result[0];
          return records;
        }).catch(function(err) {
          console.dir(err);
        });
    });
  }
};

Server.js

var cnn = require('./sqlconfig.js');
var express = require('express');
var app = express();
const bodyParser = require('body-parser');

app.set('view engine', 'ejs');
app.use(express.static('public'));


app.use(bodyParser.urlencoded({
  extended: true
}));



app.get('/', function(req, res) {
  res.render('pages/index.ejs');
});

app.post('/', function(req, res) {
  console.log({
    username: req.body.uname
  });
  console.log({
    password: req.body.upass
  });

  var r = cnn.updateMember("Kenya", "Artcaffe Nairobi", "admin1234", "127.0.0.1", "admin2");
  console.log(r); // The value of r is still undefined
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
LisaHayden
  • 43
  • 1
  • 4
  • 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) – Andreas Feb 01 '17 at 06:57

1 Answers1

0

Modify updateMember function:

  updateMember: function(Country, BankName, Password, PublicIP, UserID, callback) {
    sql.connect(config, function(err) {
      if (err) console.log("message is for" + err);
      new sql.Request()
        .input('Country', sql.VarChar(50), Country)
        .input('BankName', sql.VarChar(50), BankName)
        .input('Password', sql.VarChar(50), Password)
        .input('PublicIP', sql.VarChar(50), PublicIP)
        .input('UserID', sql.VarChar(50), UserID)
        .execute('p_LoginUser').then(function(result) {
          var records = result[0];
          // return records;
          callback.call(this, null, records); // send back result
        }).catch(function(err) {
          console.dir(err);
          callback.call(this, err);  // send back error
        });
    });
  }

Then, in server.js you can call this function:

cnn.updateMember("Kenya", "Artcaffe Nairobi", "admin1234", "127.0.0.1", "admin2", function (err, result) {
    // process result here
});
Santanu Biswas
  • 4,699
  • 2
  • 22
  • 21