简体   繁体   中英

Nesting modules with node.js

I have one module called UserProvider that looks like this

var UserProvider = function(db) { ... }
UserProvider.prototype.createUser = function(email, password, callback) { ... }
UserProvider.prototype.findUserByEmail = function(email, callback) { ... }
...
exports.UserProvider = UserProvider;

And another module called ModelProvider that looks like this

var UserProvider = require('./user').UserProvider;
var ModelProvider = function() {
    ...
    this.User = new UserProvider(db);
}
exports.ModelProvider = ModelProvider;

But the line this.User = new UserProvider(db); doesn't allow me to access the UserProvider object in my main module that has included the ModelProvider module.

When I try to call this:

var ModelProvider = require('./model/model').ModelProvider;
var Model = new ModelProvider();

Model.User.findUserByEmail(email, function() {...});

It gives the following error:

TypeError: Object function Model(doc, fields, skipId) {
    if (!(this instanceof Model))
        return new Model(doc, fields, skipId);
    model.call(this, doc, fields, skipId);
} has no method 'findUserByEmail'

I'm assuming there is some JavaScript trickery that I am missing to expose this down?

Thanks!

What appears to be happening in this case and the cause of your error is that Model.User is actually not an instance of UserProvider but appears to point to a function called Model defined as...

function Model(doc, fields, skipId) {
    if(!(this instanceof Model))
        return new Model(doc, fields, skipId);
    model.call(this, doc, fields, skipId);
}

Now this assignment could be happen at three places within your code...

var Model = new ModelProvider();
// Here sets Model.User
Model.User.findUserByEmail(...);

Or...

function ModelProvider() {
    ...
    this.User = new UserProvider(db);
    // Here sets this.User
}

Or...

function UserProvider(db) {
    // Here as a return statement
}

I would guess the latter is probably where the issue is happening.


To be clear all of the following are possible in NodeJS...

Importing a constructor from a module that didn't necessarily contain it... To be clear you shouldn't do this but it is possible to chain the exports so that a module exposes items from other modules it imports. You should import modules explicitly.

module.js...

var Something = require("./something").Something;
exports.Something;

main.js...

var Something = require("./module").Something;

Repeat do not do this. Instead include what you need from the module that owns it and don't export anything the module doesn't own. I've only included this example to clarify what exports and require can do.


Access an instance created in another module... You can export the instance via the exports object as you would a constructor.

module.js...

function MySomething() {
    //...
}
exports.mySomething = new MySomething();

main.js...

var mySomething = require("./module").mySomething;

Through an instance of an imported constructor, access an instance of a constructor that it has also imported... You can expose it through the this scope in which case it will be accessible through the public members of the instance you create of the constructor you imported.

module.js...

var Something = require("./something").Something;
function MySomething() {
    this.something = new Something();
}
exports.MySomething = MySomething;

main.js...

var MySomething = require("./module").MySomething;
var mySomething = new MySomething();
mySomething.something;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM