在Nodejs中获取未定义的变量

时间:2015-10-01 08:34:08

标签: javascript node.js

我正在使用Node来运行此代码

但是当我运行此代码时,在get函数中从leadid变量获取未定义的返回值:

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;

这就是我在主文件中调用它的方式:

var call = require('./call');
    call.hangup.get(true, ami, client, validator, growly, connection, request);
当我在主文件中调用此函数(搜索)时,

另一方面

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

它正常工作

我该如何解决?

1 个答案:

答案 0 :(得分:2)

您需要记住JavaScript是异步的。

当调用searching时,数据库查询是异步发出的,函数在数据库为您提供结果之前返回。换句话说,您的searching函数不会为您提供

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

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

    }
}

如你所愿,

   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 },
    };

它不会明确返回任何内容,因此undefined

您应该使用promises或传入searching一个回调函数,该函数将在您的数据库查询返回时被调用。

所以你的解决方案应该是这样的:

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...
}

看看jquery's promises与承诺做同样的事情。