Node JS - TypeError:无法读取属性' CONTEXT_1'未定义的

时间:2018-05-21 12:28:34

标签: node.js

文件名:orderManager.js

const notificationManager = require("./notificationManager");

var orderManager = (function() {

    function orderManager() {
        console.log("orderManager");
    };

    orderManager.bootstrap = function() {
        console.log("orderManager.bootstrap");
        return new orderManager();
    };

    orderManager.prototype.orderContext = {
        "CONTEXT_1": "context_1",
        "CONTEXT_2": "context_2",
        "CONTEXT_3": "context_3"
    };

    orderManager.prototype.notify = function() {
        var dataObj = {};
        notificationManager.sendToClient(dataObj);
    };

    return orderManager;

}());
module.exports = orderManager.bootstrap();

文件名:notificationManager.js

const orderManager = require("./orderManager");

var notificationManager = (function() {

    function notificationManager() {
        console.log("notificationManager");
    };

    notificationManager.bootstrap = function() {
        console.log("notificationManager.bootstrap");
        return new notificationManager();
    };

    notificationManager.prototype.sendToClient = function(dataObj) {
        console.log("notificationManager.prototype.sendToClient");

        var _this = this;
        switch (_this.request.body.orderContext) {
            case orderManager.orderContext.CONTEXT_1:
                notifyClient(_this, dataObj);
                break;

        }
    };

    return notificationManager;

}());
module.exports = notificationManager.bootstrap();

当我尝试从 orderManager.js 文件调用 notificationManager.sendToClient()函数时,会出现错误 TypeError:无法读取属性&#39 ; CONTEXT_1'未定义的

1 个答案:

答案 0 :(得分:1)

您不能将orderManager.js和notificationManager.js紧密联系在一起。

根据您的代码段,您尝试执行orderManager.notify().

您的Javascript编译流程将如下

  • 需要orderManager.js
  • 在orderManager.js
  • 中需要const notificationManager = require("./notificationManager");
  • 在notificationManager.js中需要const orderManager = require("./orderManager");
  • 由于orderManager.js未完全执行,因此会为其分配空对象{}而不是orderManager const orderManager = {} // Not an instance of orderManager的实例
  • 由于分配了空对象,因此在访问orderManager.orderContext.CONTEXT_1
  • 时会抛出undefined

这里我修改了你的片段并删除了orderManager.js中的notificationManager.js依赖关系

<强> orderManager.js

  var orderManager = (function() {

        function orderManager() {
            console.log("orderManager");
        };

        orderManager.bootstrap = function() {
            console.log("orderManager.bootstrap");
            return new orderManager();
        };

        orderManager.prototype.orderContext = {
            "CONTEXT_1": "context_1",
            "CONTEXT_2": "context_2",
            "CONTEXT_3": "context_3"
        };

        return orderManager;

    }());
    module.exports = orderManager.bootstrap();

文件名:notificationManager.js

const orderManager = require("./orderManager");

var notificationManager = (function() {

    function notificationManager() {
        console.log("notificationManager");
    };

    notificationManager.bootstrap = function() {
        console.log("notificationManager.bootstrap");
        return new notificationManager();
    };

    notificationManager.prototype.sendToClient = function(dataObj) {
        console.log("notificationManager.prototype.sendToClient");

        var _this = this;
        switch (_this.request.body.orderContext) {
            case orderManager.orderContext.CONTEXT_1:
                notifyClient(_this, dataObj);
                break;

        }
    };

    return notificationManager;

}());
module.exports = notificationManager.bootstrap();

文件名:index.js

const a = require('./notificationManager');
a.sendToClient();