2015-10-01 80 views
1

我使用節點來運行該代碼越來越不確定變量的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); 

它的正常工作

如何解決呢?

+2

如果'searching'是'call.hangup'的一個方法,爲什麼你要'new self.searching'?你在學習中錯過了一些東西...... – MysterX

+0

@MysterX改變了它,但錯誤是一樣的:/ – Alyreza

+1

你能解決你的問題/理解它爲什麼返回'undefined'? – Pio

回答

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做同樣的事情的承諾。

+0

你能給我一些例子或資源嗎? – Alyreza

+1

我剛纔問了一個[類似問題](http://stackoverflow.com/questions/15852043/mongoose-find)。你可以找到詳細的解釋,如[mozilla文檔](https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/Using_js-ctypes/Declaring_and_Using_Callbacks)或谷歌/搜索stackoverlow「JavaScript回調」。 – Pio

+1

雖然mozilla文檔可能不是最好的開始。 – Pio