【问题标题】:Node JS - TypeError: Cannot read property 'CONTEXT_1' of undefined节点 JS - TypeError:无法读取未定义的属性“CONTEXT_1”
【发布时间】:2018-05-21 21:17:56
【问题描述】:

文件名: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: Cannot read property 'CONTEXT_1'的未定义

【问题讨论】:

    标签: node.js


    【解决方案1】:

    您不能将 orderManager.js 和 notificationManager.js 紧密耦合。

    根据您的 sn-p,您正在尝试执行 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

    这里我修改了你的 sn-p 并删除了 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();
    

    【讨论】:

      猜你喜欢
      • 2017-03-12
      • 2018-11-30
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      • 2017-09-10
      相关资源
      最近更新 更多