question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OverwriteModelError: Cannot overwrite `xxx` model once compiled.

See original GitHub issue

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?😃

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:21 (3 by maintainers)

github_iconTop GitHub Comments

87reactions
tgdncommented, Oct 7, 2017

The way I did it is removing the model from mongoose’s models with

delete mongoose.connection.models['client'];

const clientSchema = mongoose.Schema({
  ...
});

export default mongoose.model('client', clientSchema);

Then you can register the model again through HMR or anything else.

24reactions
kppfcommented, Oct 7, 2017

@dpblh I fixed it like this:

export default (mongoose.models && mongoose.models.User
  ? mongoose.models.User
  : mongoose.model('User', userSchema));

Read more comments on GitHub >

github_iconTop Results From Across the Web

cannot overwrite model once compiled serverless
', name: 'OverwriteModelError' } It looks like it's asking Mongoose to define the same model twice, whereas if we used require both times...
Read more >
How to modify existing schema in mongoose? - Stack Overflow
OverwriteModelError : Cannot overwrite `post` model once compiled. [0] at new OverwriteModelError (C:\Users\prate\PRACTISE ...
Read more >
Cannot overwrite `xxx` model once compiled
OverwriteModelError : Cannot overwrite `xxx` model once compiled. CLEAN CODE 2019. 9. 5. 10:59. mongoose에서 발생하는 오류로.
Read more >
stryker-mutator/stryker - Gitter
Or is mongoose called from inside the 'barcode.model.js' file ? ... gives the error: OverwriteModelError: Cannot overwrite `barcodes` model once compiled ...
Read more >
Cannot overwrite `order` model once compiled._一颗小新星的 ...
解决nodejs.OverwriteModelError: Cannot overwrite order model once compiled.运行egg时出现这个错误,仔细检查报错后明白时命名model时出现了错误 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found