3

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!

7
  • I think you would need to require the user module in the main module as well or also add the UserProvider to the exports object in the model module. Commented Oct 13, 2012 at 18:33
  • Right, but instantiating an instance of the user model depends on the database connection object, So I want the Model module to initialize it on its startup, and then expose it. Commented Oct 13, 2012 at 18:35
  • So presumably you have something like var model = new ModelProvider() in your main module, if you access model.User what do you get? Commented Oct 13, 2012 at 18:37
  • Original question edited with the error message Commented Oct 13, 2012 at 18:39
  • Looks like you have some sort of name clash going on there, the error seems to suggest you have a class named Model which it may be confusing with the instance you have created of ModelProvider Commented Oct 13, 2012 at 18:46

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.