bradtraversy / node_passport_login

Node.js login, registration and access control using Express and Passport
1.74k stars 1.29k forks source link

OverwriteModelError: Cannot overwrite `User` model once compiled. #53

Open Md-Mudassir opened 4 years ago

Md-Mudassir commented 4 years ago

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

Md-Mudassir commented 4 years ago

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/

skidi3 commented 4 years ago

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 commented 3 years ago

Thanks Sir for the solution..