react-starter-kit: OverwriteModelError: Cannot overwrite `xxx` model once compiled.
Hi Guys. I have a problem. I understand what is going on, but I don’t know how it fixed.
I’m using mongoose as common ORM instead sequelize. It work not bad. But when HMR started, I have seen error in my console.
(node:29695) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): OverwriteModelError: Cannot overwrite Clientmodel once compiled.
Mongoose doesn’t allow to define the model twice. When HMR tries to update server, it defines model twice.
Can you save me?😃
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 21 (3 by maintainers)
The way I did it is removing the model from mongoose’s models with
Then you can register the model again through HMR or anything else.
@dpblh I fixed it like this:
This approach worked for me.
let UserSchema = null;
try { UserSchema = mongoose.model(‘User’, userSchema); } catch (e) { UserSchema = mongoose.model(‘User’); }
export default UserSchema;
Hi @dpblh Personally I don’t like magick like this, it will eat you in production. You have an options:
This is starter kit, not ready to use CMS, so you SHOULD make changes
Thank you very much @frenzzy, but it didn’t work for me. Because model have deep integration into server app. And I’d like to update my model in runtime. Or I don’t understand something:). I found a solution that helped me.
mongoose.js
client.js
So my model has always a new clean connection. I hope it’ll help somebody!
For me, i was requiring the model like this: var Book = .require(‘./models/Book.model’);
The name of the model is actually ‘book.model.js’ so the uppercase B caused the problem…
After i don the following all worked fine:
var Book = .require(‘./models/book.model’);
You can try to init model once inside
tools/start.jsscript for the development mode Or even better you can add HMR for specific files:Read more: https://webpack.js.org/api/hot-module-replacement/
@pols63 You will always need split points. This stack is ready to be chopped to peaces, splitted, you must know when, it’s only your responsibility 😉 Is I said above, against dislikes: No magic cannot help you unboudlesly. Get bigger and break this stack to pieces. It should be easy if you are good architect 😉 Break out backend. If you can do it, you will learn why this stack is so awesome as starter…
If you want to overwrite the existing class for different collection using typescript then you have to inherit the existing class from different class.
export class User extends Typegoose{ @prop username?:string password?:string }
export class newUser extends User{ constructor() { super(); } }
export const UserModel = new User ().getModelForClass(User , { schemaOptions: { collection: “collection1” } });
export const newUserModel = new newUser ().getModelForClass(newUser , { schemaOptions: { collection: “collection2” } });