2013-05-04 56 views
1

我使用快遞框架和node.js中試圖寫一個RESTAPI我面臨着一個我無法找出根本原因的錯誤。我收到以下錯誤試圖執行代碼:錯誤,而使用異步中的node.js

TypeError: Cannot read property 'node_type' of undefined其中「NODE_TYPE」是來自一個函數的值

var GdbProcess = require('../../dao/gdb/processnds') 
var mongo = require('mongodb'); 
var async = require('async'); 

exports.executeService = function(req,res){ 
//Make the process object to query 
var manualProcessQuery = new Object(); 
manualProcessQuery.index = req.params.processmap; 
manualProcessQuery.key = "pid"; 
manualProcessQuery.value = req.params.pid; 
manualProcessQuery.event = req.params.event; 

var tempDataNodeToExecute = new Object(); 
//This function returns an object (dataNodeToExecute) to execute 
GdbProcess.getParametersbyNode(manualProcessQuery,function(err,dataNodeToExecute){ 
    if(err) res.send(err); 
    tempDataNodeToExecute = dataNodeToExecute; 
    var isSystem = false; 
    if (tempDataNodeToExecute.node_type =="system"){ 
     isSystem = true; 
    } 

    var count = 0; 

    async.whilst(
     function() { return isSystem }, 
     function (callback) { 
      //execute the function 
      executeSystem(dataNodeToExecute,function(err,executionStatus){ 
       if (err) callback(err); 
       count++; 
       if(executionStatus=="completed"){ 
        manualProcessQuery.value = tempDataNodeToExecute.pid; 
        manualProcessQuery.event = "completed"; 
         GdbProcess.getParametersbyNode(manualProcessQuery,function(err,dataNodeToExecute2){ 
         if(err) callback(err); 
         tempDataNodeToExecute = dataNodeToExecute2; 
         if (tempDataNodeToExecute.node_type == "manual"){ 
          isSystem = false; 
         } 

        }); 

        callback(); 
       } 
      }); 
     }, 
     function (err) { 
      if(err) res.send(err); 
      res.send("success"); 
     } 
    ); 
}); 

} 



var executeManual = function(prosNodeToExecute,callback){ 
//do something 
callback (null); 
} 

var executeSystem = function(prosNodeToExecute,callback){ 
//do something 
callback(null,"completed"); 
} 

當我調試的代碼,我清楚地看到,NODE_TYPE可用。有人能幫我找到根本問題嗎?

回答

0

刪除新對象tempDataNodeToExecute和使用,而不是它dataNodeToExecute,這是一個很好的做法,使程序不崩潰使用其財產之前檢查對象的零。

+0

是..我試過dataNodeToExecute。但它的結果是一樣的。我是故意不檢查對象的空幫我找出到底崩潰 – codejammer 2013-05-04 17:52:52