Skip to content
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

OverwriteModelError: Cannot overwrite User model once compiled. #53

Open
Md-Mudassir opened this issue Oct 19, 2019 · 3 comments
Open

Comments

@Md-Mudassir
Copy link

throw new _mongoose.Error.OverwriteModelError(name);
^
OverwriteModelError: Cannot overwrite User model once compiled.

@Md-Mudassir
Copy link
Author

Md-Mudassir commented Oct 19, 2019

throw new _mongoose.Error.OverwriteModelError(name);
^
OverwriteModelError: Cannot overwrite User model once compiled.

On Windows, if we try to open a User.js or user.js file, both will point to the same location (which is what happened here). However, nodejs manages the modules themselves in a case-sensitive manner. So if we do: require ('./ User') and require ('./ User'), nodejs will try to register 2x. What duplicates User schema registration in mongoose.

On Linux / Mac this would not happen, because well before this error popped up, the node would scream saying that the module User.js does not exist. And it would be much simpler to find the error.

//Fix
under /Models folder .. rename User.js => UserDetails.js

for more info refer here: https://www.devpleno.com/corrigindo-o-erro-cannot-overwrite-model-model-once-compiled-no-mongoose/

@Md-Mudassir Md-Mudassir reopened this Oct 19, 2019
@skidi3
Copy link
Contributor

skidi3 commented Oct 25, 2019

File name is case sensitive .

  1. In "config/passport.js" file.
    Go to line 6
    "const User = require('../models/user');"
    change it to
    "const User = require('../models/User');"

  2. In "routes/users.js"
    change line 6 to "const User = require('../models/User');"

@rahuldey-anshu
Copy link

Thanks Sir for the solution..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants