-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Allow import { model } from mongoose
#3768
Comments
Supporting this behavior would involve a massive refactor of mongoose connections. Long story short, |
@vkarpov15 thanks for your answer! There are 2 different cases:
When we have the issue open, it can be fixed with breaking change (in new major release) or without breaking change (you know, everything can be refactored with backward compatibility, it just depends on effort). Someone (e.g. me) probably has time for it. But when we mark the issue closed, we give contributors to know that pull requests with fixing this bug will be rejected, and someone doesn't even start to fix it. What do you think about it? |
Fair point, I'll keep this issue open. The workaround is easy, just ensure the functions have proper context. The eventual solution is no side-effects. |
+1 Currently this doesn't work: import { model } from 'mongoose' |
Not expected to work in the immediate future because of side-effects in mongoose. No really good way to make |
I'll +1 this.
^ is a handy/attractive syntax. |
See Automattic#3768 To allow invocation of `model` in any context, I do a quick check to see if `this` within the invocation refers to the `mongoose` instance. If it does not, `Mongoose.prototype.model` is called again with the proper context.
I created #3943 as a quick fix for this. If it's acceptable (until a better solution can be created), then I can write a test to go with the PR. If it needs revised at all, I'm more than willing to. |
I like the general idea, but tests fail in that pr, which is a deal breaker unfortunately |
Yup, I did see that. I'll be playing with it tomorrow to see what's going On Sat, Mar 5, 2016, 17:45 Valeri Karpov notifications@github.com wrote:
|
Came across this today after trying needless to say it didn't work so back to Be nice if someone has time for a fix but I can see why it's not high priority! |
So, to try and clarify, running import { model } from 'mongoose';
//...
model('testModel', testModelSchema); ...exposes the import mongoose from 'mongoose';
//...
mongoose.model('testModel', testModelSchema); places the model in the scope of the already connected mongoose singleton object. Can some confirm this for me please. |
@cr05s19xx that is generally correct. In the former, |
If this error only occurs when we have this problem, it would be beneficial to print out the cause and fix so people don't end up having to google and coming to this issue like I did. |
import { model } from mongoose;
or const { model } = require('mongoose')
import { model } from mongoose;
or const { model } = require('mongoose')
import { model } from mongoose
In 5.3.14, you'll be able to do |
It's common when user wants to import variables from module without saving module into a variable, e.g.
or
but when I do this with model
and try to create a model with imported function, I have an error
Yes, model is a property of Mongoose.prototype, but Schema also is.
If documentation contains an instruction like
it's possible to assume that 'model' will have similar behavior
The text was updated successfully, but these errors were encountered: