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.