Reputation: 33
I am using angular-fullstack generator and I have added a Model person. When I try to require the person model in the seed.js file I get this error.
/Users/dev/wishlist/node_modules/mongoose/lib/index.js:334
throw new mongoose.Error.OverwriteModelError(name);
^
OverwriteModelError: Cannot overwrite `Person` model once compiled.
at Mongoose.model (/Users/dev/wishlist/node_modules/mongoose/lib/index.js:334:13)
at Object.<anonymous> (/Users/dev/wishlist/server/api/wishList/person.model.js:11:27)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/dev/wishlist/server/api/wishList/wishList.controller.js:4:14)
at Module._compile (module.js:456:26)
I followed the same structure used for the "Thing" model that comes with the generator. Also did a search and where Person is in the code base and its only in the person.model.js and in the controller.
person.model.js:
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var PersonSchema = new Schema({
name: String,
quote: String
});
module.exports = mongoose.model('Person', PersonSchema);
wishlist.controller.js:
'use strict';
var _ = require('lodash');
var Person = require('./person.model');
// Get all the People that have wish lists
exports.getPeople = function(req, res) {
Person.find(function (err, people) {
if(err) { return handleError(res, err); }
return res.json(200, people);
});
};
function handleError(res, err) {
return res.send(500, err);
}
What am I missing?
Upvotes: 3
Views: 3431
Reputation: 103
I also ran in to same error few weeks ago. After trying out few things I came out with a simple fix:
just try to export person model this way -
module.exports.PersonModel = mongoose.model('Person', PersonSchema);
instead of - module.exports = mongoose.model('Person', PersonSchema);
Hope this helps !
if you still get the error just the paths in the modules you were exporting this model.
Upvotes: 0
Reputation: 5898
As I understand from the information that you posted, the issue appears to be caused by the following lines:
at Object. (/Users/dev/wishlist/server/api/wishList/person.model.js:11:27)
module.exports = mongoose.model('Person', PersonSchema);
at Object. (/Users/dev/wishlist/server/api/wishList/wishList.controller.js:4:14)
var Person = require('./person.model');
This error most commonly comes from a mismatching between Mongoose models.
Somewhere along the road, you have defined this model under a different name, but the same Schema.
In order to see if this is the thing that causes your error, add this code in person.model.js
right after you require mongoose
:
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.models = {};
mongoose.modelSchemas = {};
This will clear out your mongoose data and empty out all existing models and schemas.
I found the aforementioned information at the following LINK.
I have also found some additional discussions that address this issue HERE and HERE.
Upvotes: 4