Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deal with circular dependencies #3826

Closed
ajsharp opened this issue Jan 31, 2016 · 7 comments
Closed

How to deal with circular dependencies #3826

ajsharp opened this issue Jan 31, 2016 · 7 comments

Comments

@ajsharp
Copy link

ajsharp commented Jan 31, 2016

I have two models:

// app/models/message.js
var User = require('./user');

var MessageSchema = new Schema ({
  sender: {type: Schema.Types.ObjectId, ref: 'User', required: true},
});

MessageSchema.methods.toJSON = function() {
  return {
    sender: User.publicJSON(this.sender)
  };
}

// app/models/user.js

var Message = require('./message')
var UserSchema = new Schema ({
});

UserSchema.methods.recentMessages = function() {
  return Message.where({sender: this.id});
};

UserSchema.statics.publicJSON = function(user) {
  return {
    id: this.id,
    recent_messages: user.recentMessages()
  }
};

Requiring one from the other causes problems and causes a copy of the model to be returned before it's fully formed. Specifically, before the User model has it's publicJSON static attached to it. Do you know of a way to deal with this issue?

@vkarpov15
Copy link
Collaborator

Hard solution: use a dependency injector. Easy solution: if you create a model using mongoose.model('Message', MessageSchema); you can then access the model with mongoose.model('Message');, so all you need to do is require('mongoose'); in the file to access your models.

@developius
Copy link

@vkarpov15 that 'easy' solution breaks down when you've got models being loaded one after the other which depend on each other - you get the error MongooseError: Schema hasn't been registered for model. Is there a way around this? I guess the best way would be a separation of concerns approach and use a controller or helper method to handle it.

@vkarpov15
Copy link
Collaborator

Can you clarify what you mean by models "which depend on each other"? If you have schemas that depend on each other, then I'd recommend you keep them in the same file, but that's a quirk of node rather than a mongoose issue

@ianpogi5
Copy link

Hard solution: use a dependency injector. Easy solution: if you create a model using mongoose.model('Message', MessageSchema); you can then access the model with mongoose.model('Message');, so all you need to do is require('mongoose'); in the file to access your models.

Would be great if you have a code sample demonstrating this. I tried it and got MongooseError: Schema hasn't been registered for model.

@vkarpov15
Copy link
Collaborator

@ianpogi5 most likely you're calling mongoose.model('message') before defining your model

@ianpogi5
Copy link

@ianpogi5 most likely you're calling mongoose.model('message') before defining your model

My code was the same as @ajsharp. I solved it by avoiding the situation using a third file to use the two models.

But still I don't get what you mean. Doing your suggestion yields the error @developius encountered. A code example modifying @ajsharp code would be great.

@pykeras
Copy link

pykeras commented Jan 29, 2022

this solves my problem, for any one may need it:

In all dependent models, require(import) your models after export current source model.

Model_A.js

module.exports = mongoose.model("ModelA", schema);
const ModelB = require("./Model_B.js");

and for Model_B.js

module.exports = mongoose.model("ModelB", schema);
const ModelA = require("./Model_A.js");

@Automattic Automattic locked and limited conversation to collaborators Jan 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants