【问题标题】:error while using async in node.js在 node.js 中使用异步时出错
【发布时间】:2013-05-05 01:48:24
【问题描述】:

我正在尝试使用 express 框架和 node.js 编写一个 restapi。我面临一个错误,我无法找出根本原因。尝试执行代码时出现以下错误:

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 是可用的。有人可以帮我在这里找到根本问题吗?

【问题讨论】:

    标签: javascript node.js asynchronous express


    【解决方案1】:

    删除新对象 tempDataNodeToExecute 并使用 dataNodeToExecute 代替它,并且在使用它的属性之前检查对象的 null 是一个很好的做法,这样程序就不会崩溃。

    【讨论】:

    • 是的.. 我试过 dataNodeToExecute。但结果是一样的。我故意不检查对象的 null 以帮助我找出它崩溃的地方
    猜你喜欢
    • 2018-07-01
    • 1970-01-01
    • 2020-07-11
    • 2014-10-31
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 2018-06-08
    相关资源
    最近更新 更多