2015-02-24 56 views
0

我爲我的Mongoose模型創建了一個名爲data_models/index.js的模塊,非常簡單。Mongoose OverwriteModelError:一旦編譯就無法覆蓋模型

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

現在,當我跑我的摩卡測試稱爲再這樣

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. 
+0

在此之前,您知道您的出口有錯字,對不對? Global_Tagas而不是Global_Tags – mrBorna 2015-02-24 18:09:43

+0

是的,這是一個錯字 – 2015-02-24 20:58:33

回答

0

錯誤發生原因您已經定義了一個模式。 查看解決方案here

+0

該解決方案說什麼? – 2017-04-17 07:43:59