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
Comments
Hard solution: use a dependency injector. Easy solution: if you create a model using |
@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 |
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 |
Would be great if you have a code sample demonstrating this. I tried it and got MongooseError: Schema hasn't been registered for model. |
@ianpogi5 most likely you're calling |
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. |
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
and for Model_B.js
|
I have two models:
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?The text was updated successfully, but these errors were encountered: