J.Arturo Covarrubias
J.Arturo Covarrubias

Reputation: 176

Mongoose OverwriteModelError: Cannot overwrite model once compiled

I created a module for my Mongoose models called data_models/index.js, is very simple.

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,

    var GlobalTagsSchema = new Schema ({
        _Id: Schema.Types.ObjectId ,
        tag_name: {type: String, require: true, unique: true},
        createdDate : { type: Date, default: Date.now } ,
        alias : [{
            tag_name: {type: String},
            createdDate: {type: Date, default: Date.now}
        }]

    });

module.exports = {
        InitDB:function(user,pass){
            var conn = mongoose.connect('mongodb://'+user+':'+pass+'@localhost/db');
            var db = mongoose.connection;
            db.on('error',console.error.bind(console, 'connection error ....'));
            db.once('open',function callback(){
                console.log(' Database connected..');
            });
            return db ;
        },
        Global_Tagas : mongoose.model('Global_Tags', GlobalTagsSchema)
}

Now when I run my test in Mocha is called then this way

   var nebulab_data_model = require('nebulab_data_models');
       nebulab_data_model.InitDB(process.env.MONGODB_USER,process.env.MONGODB_PASSWORD);

When I run my test I get the following error :

/Users/Tag/node_modules/mongoose/lib/index.js:334
      throw new mongoose.Error.OverwriteModelError(name);
            ^
OverwriteModelError: Cannot overwrite `Global_Tags` model once compiled.

Upvotes: 1

Views: 15587

Answers (3)

Paul Chibueze
Paul Chibueze

Reputation: 1

This is a tricky situation and I just spent 7 hours trying to debug this issue as of today after I couldn't use the one provided here.

Here is what helped me and I got it from this official site https://mongoosejs.com/docs/faq.html#overwrite-model-error

For example, remember that mongoose.model('ModelName', schema) requires 'ModelName' to be unique, so you can access the model by using mongoose.model('ModelName'). If you put mongoose.model('ModelName', schema); in a mocha beforeEach() hook, this code will attempt to create a new model named 'ModelName' before every test, and so you will get an error.

Make sure you only create a new model with a given name once. If you need to create multiple models with the same name, create a new connection and bind the model to the connection.

For Example ;

const mongoose = require('mongoose');
const connection = mongoose.createConnection(/* ... */);

// use mongoose.Schema
const kittySchema = mongoose.Schema({ name: String });

// use connection.model
const Kitten = connection.model('Kitten', kittySchema);

Upvotes: 0

GMKHussain
GMKHussain

Reputation: 4681

Export this way when use same model multiple times

module.exports = mongoose.models['Global_Tags'] || mongoose.model('Global_Tags', GlobalTagsSchema)

Upvotes: 0

Shriganesh Kolhe
Shriganesh Kolhe

Reputation: 253

The error is occurring because you already have a schema defined. check out the solution here

Upvotes: 2

Related Questions