javascript - 在 Node.js 中使用异步时出错

标签 javascript node.js asynchronous express

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

类型错误:无法读取未定义的属性“node_type”,其中“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可用。有人可以帮我找到根本问题吗?

最佳答案

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

关于javascript - 在 Node.js 中使用异步时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16377100/

相关文章:

node.js - MongoDB 填充性能

javascript - Mongodb 并行运行数千个查询

Javascript\Kango : How to get a return of an async JS function inside a non-async function?

javascript - 在 jQuery 中设置 cookie 后无法重定向?

javascript - 默认情况下将 sap.ui.commons.DatePicker 设置为当前日期

node.js - Angular 6 文件上传

c# - 调用套接字的ReceiveAsync()调用后,接收到的数据缓冲区始终为空吗?

android - kotlin 异步异常处理

javascript - 为什么这个 HTML5 地理位置相关的 javascript 代码会出错?

javascript - 具有命名 View 的 UI 路由器查询参数