Mongoose OverwriteModelError:编译后无法覆盖模型

时间:2015-02-24 04:58:21

标签: node.js mongodb mongoose

我为我的Mongoose模型创建了一个名为data_models / index.js的模块,非常简单。

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)
}

现在当我在Mocha中运行我的测试时,就这样调用了

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

当我运行测试时,我收到以下错误:

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

1 个答案:

答案 0 :(得分:0)

发生错误是因为您已经定义了架构。 查看解决方案here