Next.js Discord

Discord Forum

Mongoose plugins

Unanswered
Brown Thrasher posted this in #help-forum
Open in Discord
Avatar
Brown ThrasherOP
How to properly register plugins with mongoose and next.js. The plugin I currently want to add is mongoose-paginate-v2, but I imagine that this is related to any plugin. The issue starts with mongoose model registration:
UserSchema.plugin(paginate); 
export const User = mongoose.model<User, mongoose.PaginateModel<User>>("users", UserSchema);

This throws error: "OverwriteModelError: Cannot overwrite users model once compiled."
To solve this, I added models check:
export const User = mongoose.models.users || mongoose.model<User, mongoose.PaginateModel<User>>("users", UserSchema);

And this now creates issue with paginate plugin: "_models_UserWEBPACK_IMPORTED_MODULE_1.User.paginate is not a function"

0 Replies