Alyreza
Alyreza

Reputation: 91

getting undefined variable in Nodejs

I'm using Node to run this code

but when I run this code get the undefined return from leadid variable in get function :

call = {};
call.hangup = {
    get: function(isOpen, ami, elastic, validator, growly, mysql, request){
        var self = call.hangup;
        this.isOpen = isOpen;
        this.ami = ami;
        this.elastic = elastic;
        this.validator = validator;
        this.growly = growly;
        this.mysql = mysql;
        this.request = request;
        if(isOpen)
        {
            ami.on('hangup', function(evt){
                var cause_status = parseInt(evt.cause);
                var message = self.causeStatus(cause_status);
                var channel = evt.channel;
                var isDAHDI = validator.contains(channel,"DAHDI");
                if((isDAHDI)&&(cause_status == 16)) 
                {

                    var mobile = evt.calleridnum;
                    if(validator.isLength(mobile,11))
                    {
                        if(validator.matches(mobile,/^09/i))
                        {   
                            var txtMessage = "";
                            var sending = "";
                            var leadid = self.searching(mobile, mysql, validator);

                           retrun leadid; ///  get the undefined !!!!


                        }
                    }
                }

            });
        }else {
            console.log("Hangup's Event is OFF !");
        }       
    },
searching: function(number, mysql, validator){
    this.number = number;
    this.mysql = mysql;
    this.validator = validator;
    var query = "{sql ...}";

    mysql.query(query, function(err, rows, fields) {        
    if (err) throw err; 
        if(!validator.isNull(rows))
        {               

            return rows[0].leadid;
        }else {
            return false;

        }
    });
},
};
module.exports = call;

this is how I call it in main file:

var call = require('./call');
    call.hangup.get(true, ami, client, validator, growly, connection, request);

in other hands when I call this function (searching) in the main file

new call.hangup.searching(number, connection, validator);

it's work correctly

how can I fix it ?

Upvotes: 1

Views: 1319

Answers (1)

Pio
Pio

Reputation: 4064

You need to keep in mind that JavaScript is asynchronous.

When searching is called the database query is issued asynchronously and the function returns before the database would give you a result. In other words your searching function does not give you the result from

function(err, rows, fields) {        
if (err) throw err; 
    if(!validator.isNull(rows))
    {               

        return rows[0].leadid;
    }else {
        return false;

    }
}

as you would desire,

   searching: function(number, mysql, validator){
    this.number = number;
    this.mysql = mysql;
    this.validator = validator;
    var query = "{sql ...}";

    mysql.query(query, function(err, rows, fields) {        
    if (err) throw err; 
        if(!validator.isNull(rows))
        {               

            return rows[0].leadid;
        }else {
            return false;

        }
    });
     --- > This is reached at the end of the call and nothing is returned },
    };

it does not return explicitly anything, hence the undefined.

You should either use promises or pass in searching a callback function that will be called when your database query returns.

So your solution should look like this:

get: function (...){
  ...
  self.searching(mobile, mysql, validator, whatIwantToDoAfterValidation)
},
searching: function(number, mysql, validator, callback){
   ...
   mysql.query(..., callback){
    if(!validator.isNull(rows))
    {               
        callback(rows[0].leadid);
    }else {
        callback(false);
    } 
   }
},
whatIwantToDoAfterValidation: function(leadid){
  ...do whatever you want with leadid...
}

Take a look at jquery's promises to do the same thing with promises.

Upvotes: 2

Related Questions