我使用節點來運行該代碼越來越不確定變量的NodeJS
但是當我運行此代碼得到leadid變量get函數未定義回報:
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);
它的正常工作
如何解決呢?
如果'searching'是'call.hangup'的一個方法,爲什麼你要'new self.searching'?你在學習中錯過了一些東西...... – MysterX
@MysterX改變了它,但錯誤是一樣的:/ – Alyreza
你能解決你的問題/理解它爲什麼返回'undefined'? – Pio