Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of Error: "OverwriteModelError: Cannot overwrite `undefined` model once compiled."?

Tags:

I have a common method for updating document of any collection in MongoDB?

The following code is in file name Deleter.js

module.exports.MongooseDelete = function (schemaObj, ModelObject); {   var ModelObj = new mongoose.Model("collectionName",schemaObj);   ModelObj.remove(ModelObject); } 

And invoking as follows in my main file app.js:

var ModObj = mongoose.model("schemaName", schemasObj); var Model_instance = new ModObj(); var deleter = require('Deleter.js'); deleter.MongooseDelete(schemasObj,Model_instance); 

I am getting following error:

OverwriteModelError: Cannot overwrite `undefined` model once compiled.     at Mongoose.model (D:\Projects\MyPrjct\node_modules\mongoose\lib\index.js:4:13) 

I get on 2nd method call only.. Please let me know if any one has got some solution.

like image 706
Amol M Kulkarni Avatar asked Feb 01 '13 07:02

Amol M Kulkarni


1 Answers

I managed to resolve the problem like this:

var Admin;  if (mongoose.models.Admin) {   Admin = mongoose.model('Admin'); } else {   Admin = mongoose.model('Admin', adminSchema); }  module.exports = Admin; 
like image 135
paulbjensen Avatar answered Oct 16 '22 05:10

paulbjensen