Reputation: 3
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.
Upvotes: 0
Views: 3028
Reputation: 203231
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.
Upvotes: 1
Reputation: 812
var mongoose = require('mongoose');
let Deposit = mongoose.model('Deposit');
use above two line instead : let Deposit = require('../../models/deposit');
Upvotes: 2
Reputation: 366
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
Upvotes: 0
Reputation: 3
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...
Upvotes: 0
Reputation: 2769
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);
?
Upvotes: 0