0

您好,我的 express nodejs 应用程序中有 3 个文件:文件 FactorySchool.js:

 require("./OBforSchool.js");
function FactorySchool () {
}
FactorySchool.prototype.dofingSchool = function () {
        console.log(this.message);
        return this.message;
    };
 FactorySchool.factorySchool = function (fncontrol) {
        var contr = fncontrol[1];
        var varSchool;
        if (typeof FactorySchool[contr] != "function") {
            console.log("Undifine function");
        } else {
            if (typeof FactorySchool.prototype.dofingSchool != "function") {
                FactorySchool[contr] = new FactorySchool();
            }
            varSchool = new FactorySchool[contr](fncontrol[0]);
        }
        return varSchool;

    };

 //--------------------DEfine method to process school
    FactorySchool.addBlockSC = function (OBblock) {
        return this.message = "add OK";
    }
    FactorySchool.deleteSC = function (OBblock) {
        this.message = "dekete OK";
    }

文件 School.js

 //file School.js to call
    var facschool = require('./FactorySchool');
        var obchool = require('./OBforSchool');
        exports.school = function (req, res) {
            var obsc = new Object();
            var detailSchoolob = new Object();
            detailSchoolob.firstname = "Thai";
            detailSchoolob.lastname = "Ngo";
            obsc.agr1 = "addBlockSC";
            obsc.agr2 = detailSchoolob;
            var a = new facschool.FactorySchool.factorySchool(obsc);
            console.log(a);
            a.dofingSchool();

        };

当我打电话时

var a = new facschool.FactorySchool.factorySchool(obsc);

我收到一个错误:500 TypeError: Cannot read property 'factorySchool' of undefined

我该如何解决。谢谢!!!!

4

1 回答 1

0

FactorySchool.factorySchoolFactorySchool构造函数中定义。您应该在访问定义的属性之前调用它。

但是,如果您在构造函数之外定义它会更好。

于 2013-09-23T07:09:08.140 回答