1

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 ?

5
  • 2
    Why do you do new self.searching if searching is a method of the call.hangup? You are missed something in your studying... Commented Oct 1, 2015 at 8:39
  • @MysterX changed it, but error was the same :/ Commented Oct 1, 2015 at 8:45
  • 1
    Were you able to solve your problem/understand why it did return undefined? Commented Oct 1, 2015 at 15:52
  • @pio what is your mean ? Commented Oct 1, 2015 at 16:32
  • 1
    I was trying to suggest that if my answer helped you in solving your problem please accept the answer. If you did not manage to solve the issue please follow up with your progress/where are you stuck. Commented Oct 1, 2015 at 16:48

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

can you give me some example or resources ?
I asked a similar question a while ago. You can find detailed explanations like mozilla docs or google/search on stackoverlow "javascript callbacks".
The mozilla docs may not be the best thing to start with though.
How can I handle this example with your answer ?
I think it is best if you postpone your development with a few hours and get to the bottom of understanding how asynchronous functions work. There are plenty of resources, e.g. this. In short what I meant with the above phrase is: you don't know when query in searching finds the db record that your validation needs. But you want to do some action when it does so you need to pass in a function to be called when that happens. Asynchronous languages can be a bit tricky to understand at first.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.