0

I have declared class which generates the records and simply return it but it is returning undefined.

var classObj = new User();
var user = classObj.generateUserResponse(val1,mode);
console.log(user) //undefined

User.prototype.generateUserResponse = function (userid, mode) {

     conn.query('select * from users where user_id = ?', [userid], function (err, user) {
              if(mode == 'true') {
                   var genObj = new CustomerJSON();
                   userObj = genObj.generateUserCustomer(user);
                   console.log(userObj) // displays the value
                   return userObj;
              } else {
                // do something else
}
    })

What I am doing above is creating a new class and passing a user object in a new class for further processing. I can console log and can see the data but when I return userObj it displays undefine.

JN_newbie
  • 5,492
  • 14
  • 59
  • 97
  • 3
    Your `return userObj;` statement is returning a value for the function in the conn.query line. – IrkenInvader Apr 12 '16 at 17:59
  • @IrkenInvader, it means it should be outside the conn.query statement? – JN_newbie Apr 12 '16 at 18:03
  • Your prototype method is not returning anything. The only `return` you have is for the `conn.query` callback. What do you want `generateUserResponse` to return? It looks like its asynchronous. – NicolasMoise Apr 12 '16 at 18:08
  • I'm not sure exactly, just meant that your function is returning undefined because you never return anything in the outer function. Maybe conn.query returns a value so you can just add return to the start of that line? – IrkenInvader Apr 12 '16 at 18:08
  • @NicolasMoise. conn.query is asynchronus but how to return a value of the prototype method then? – JN_newbie Apr 12 '16 at 18:12
  • you could `return conn.query(....)`, or whatever you want. Really, you should ask yourself what do YOU want that function to return? Does it even make sense for that function to return anything? why? – NicolasMoise Apr 12 '16 at 18:15

2 Answers2

1

It is right, because the invoked asynchronous function calls the callback (which returns the object to noone) after you log the object to console.
The function generateUserResponse returns no value.

Solution: make the required actions in the callback function.

Pavel Gatnar
  • 3,987
  • 2
  • 19
  • 29
1

Pavel is correct. Your function is asynchronous. You need a callback. Try something like this:

var classObj = new User();
classObj.generateUserResponse(val1, mode, function(userObj) {
  console.log(userObj);
});

User.prototype.generateUserResponse = function(userid, mode, callback) {
  //you could decide to return conn.query here and handle it differently above
  conn.query('select * from users where user_id = ?', [userid], function(err, user) {
      if (mode == 'true') {
          var genObj = new CustomerJSON();
          userObj = genObj.generateUserCustomer(user);
          console.log(userObj) // displays the value
          return callback(userObj);
      } else {
          // do something else 
          //THEN
          return callback(userObj);
      }
  })

});

Or you could return conn.query(... and handle the promise it returns based on whatever library you're using.

Michael Tallino
  • 821
  • 4
  • 16