3

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?

user1801680
  • 33
  • 1
  • 3
  • i'm running into the same problem, and though the suggestion below seems to "fix" my problem it's hard to believe that would be the right way to fix it. – Randy L Jun 05 '15 at 20:19

2 Answers2

4

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.

Community
  • 1
  • 1
vladzam
  • 5,462
  • 6
  • 30
  • 36
  • Thanks Vlad that fixed it. I am concerned with this solution though as this could empty out the other schemas I create. I read the articles you linked and none of them really fix my problem. I understand what is going wrong I just cant figure out where it is going wrong. As far as I can tell I am only defining the schema once. – user1801680 Dec 14 '14 at 01:19
0

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.

Ps Naidu
  • 103
  • 1
  • 5