1

I have a rather weird issue with NodeJS, require and mongoose. I created a schema for a user model like this:

let mongoose = require('mongoose'); let Schema = mongoose.Schema; let depositSchema = new Schema({ customer: String, tyres : { amount: { type: Number, min: 1, required : true}, tyreType : { type: String, required : true } }, created : { at : { type: Date, default : Date.now }, by : { type : Schema.ObjectId } }, last_modified : { at : { type: Date }, by : { type: Schema.ObjectId } }, located_at : { column: { type: String, required : true }, row: { type: String, required : true } } }); depositSchema.pre('save', function(next) { let date = new Date(); this.last_modified.at = date; if(!this.created.at) { this.created.at = date; } next(); }); module.exports = mongoose.model('Deposit', depositSchema); 

So you can see the file exports the mongoose model. If I require this file in another one like this:

let Deposit = require('../../models/deposit); 

Everything is fine. Everything works and I have no issues using the model and creating objects from it to save it in Mongo.

But if I require the model like this:

let Deposit = require('../../models/Deposit); 

I get this error from Mongo:

 /app/node_modules/mongoose/lib/index.js:376 throw new mongoose.Error.OverwriteModelError(name); ^ MongooseError: Cannot overwrite `Deposit` model once compiled. 

Error points at the line I am requiring the model.

I searched for similar issues with require but did not find anything useful what explains my problem. The same problem occurs with another model but in different direction in spelling. I am very confused. Maybe someone had the same issue or is able to explain what happens / happened and what caused the issue.

Thank you in advance.

Have a successful week everybody.

5 Answers 5

2
 var mongoose = require('mongoose'); let Deposit = mongoose.model('Deposit'); 

use above two line instead : let Deposit = require('../../models/deposit');

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @Love-Kesh for your answer. It works fine. I would like to know where the actual difference is between the two ways of receiving the model ? The one I used is used by a lot of people who write that in their blogs and tutorials.
Ones you defined model using mongoose.model('Deposit', depositSchema); there is no need to execute that again. By using require your file get execute each time when ever you use require. So the way of getting it is : let Deposit = mongoose.model('Deposit');
Okay, good to know that. Thank you very much for your answer and explanation
1

The problem is that the Node require cache doesn't know that ../../models/deposit and ../../models/Deposit are actually the same file.

If it would know that they are, it would not have loaded and executed the file again, but instead return the previous exported value from the first time it was loaded (which is stored in the cache).

However, because it sees these files are being different, it will load and execute the file twice, and the second time it will cause the error being thrown (because it runs the "create-a-model" code twice, which Mongoose doesn't like).

There are a few workarounds, but ultimately, you should use the proper filename (how it appears in Finder/Explorer/whatever) every time you require() the file.

1 Comment

I think this is the correct answer. In my case I was requiring the same file in two different ways (once with a relative URL, from inside the package, and once with a package name, from outside the package), and the require cache must have thought they were two different files.
0

I believe the problem is that you define a model called Deposit on this line of code module.exports = mongoose.model('Deposit', depositSchema); and then you try to re-define it again using a variable with the same name Deposit.

Did you try to do something like var deposit = requrire(../../models/deposit);?

4 Comments

Thanks for your super fast answer. I tried it with var, but got the same issue. I have a user model, which is designed like the deposit model and I tried requiring it with var and different spelling types (lower / upper ) but got the same error from mongo...
The problem is not the var, is the lower case in the name of the variable.
Requiring the deposit model I currently use the lower case spelling, as it only works like this.
That's what I'm telling you. When you define the define the Schema, you are defining it using upper case. So when you say let Deposit = ... it's like you're re-defining it. Make the following test: Change the name of the mongoose model like this mongoose.model('Deposit1', depositSchema); and try again. I bet you won't have the problem.
0

I tried what Israel.zinc suggested and renamed the deposit model in 'Deposit1' in the export line. Afterwards I tried to require it like this :

let Deposit = require('../../models/Deposit'); 

And received the exact same Mongoose error except it saying "Cannot overwrite 'Deposit1'..." If I require it like this:

let Deposit = require('../../models/deposit'); 

Everything works fine...

Comments

0

whats the name of file in which you are defining your usermodel ?? in my opinion it may be just the file name you are requiring wrong

1 Comment

It is 'user.js' and for deposit it is 'deposit.js' , that's the point I don't get, it would be obvious if I named them differently but both are lowercase and the exact same is exported in uppercase ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.