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
Fail to register global plugin #7723
Comments
this is what you can do mongoose.plugin(function(schema) {
const localTransform = (schema.get("toJSON") || {transform: (doc, ret) => ret}).transform;
schema.set("toJSON", {
transform(doc, ret) {
return globalTransform(localTransform(doc, ret));
},
});
}); |
Hello, Thank you very much for your help, sadly the global plugin still does not works for me with the code you posted. |
@LouisGirones can you please show how you're defining your user model? Global plugins aren't applied until you call |
Yes sure, I have this file for starting the node server in the root folder (
And in
and
So I would think that calling So I might have to change where I call |
@LouisGirones you need to call |
thanks for your great point |
|
I fell into this trap and this comment cleared my issue. Is this fact mentioned anywhere in the mongoose docs? I feel like I could have saved myself from stress if I had seen this earlier. |
@Eunovo we made a point of this in the middleware docs, but we should also make a note in the plugins docs because many plugins rely heavily on middleware. |
*Before creating an issue please make sure you are using the latest version of mongoose
Do you want to request a feature or report a bug?
Report a bug
What is the current behavior?
Using a plugin in every model works, but using the plugin globally aka
mongoose.plugin(myPlugin)
does notIf the current behavior is a bug, please provide the steps to reproduce.
I am linking this issue since it seems kinda related :
#7042 (comment)
I've created a very basic plugin :
UserSchema.plugin(myPlugin)
myPlugin is added to the UserSchema as it shouldmongoose.plugin(myPlugin)
myPlugin should be added for all schemaHowever the
mongoose.plugin(myPlugin)
does not works while addingUserSchema.plugin(myPlugin)
worksWhat is the expected behavior?
Using
mongoose.plugin(myPlugin)
should have the same results as addingmyModel.plugin(myPlugin)
in every of my schema.What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node version: 11
mongodb version : 4
mongoose version : 5.5.2
The text was updated successfully, but these errors were encountered: