Trouble with "OverwriteModelError: Cannot overwrite … model once compiled" #847
Replies: 3 comments 1 reply
-
Here's the full error stack:
|
Beta Was this translation helpful? Give feedback.
-
Maybe I found the solution from https://nesin.io/blog/fix-mongoose-cannot-overwrite-model-once-compiled-error? I replaced my this statement: const Platform = getModelForClass(PlatformClass);
export default Platform; with this: export const Platform =
mongoose.models.Platform || getModelForClass(PlatformClass); And I think it's working as expected now! |
Beta Was this translation helpful? Give feedback.
-
from what i can tell, it looks like some kind of "hot-reloading" is the problem, it reloads the model file and wants to re-create the model, but does not reload the actual mongoose module (and so its already created models) and wants to add a model with the same name on the same connection / mongoose instance, which mongoose does not support workarounds would be:
|
Beta Was this translation helpful? Give feedback.
-
Please help! I feel like I'm going crazy trying to figure this one out.
I'm getting the error "OverwriteModelError: Cannot overwrite 'Platform' model once compiled" on my project.
I'm using Typegoose 11.2.0, Mongoose 7.2.2, and Next.js 13.4.4.
Here's my Platforms model file:
I don't call this model in many places. But I keep getting the above error after some page refreshes.
contollers.platforms.ts:
services/platforms/app1.ts:
What am I doing wrong?
Beta Was this translation helpful? Give feedback.
All reactions