Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't fix mongoose overwrite model error

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?

like image 303
user1801680 Avatar asked Mar 25 '26 07:03

user1801680


1 Answers

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.

like image 70
vladzam Avatar answered Mar 27 '26 21:03

vladzam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!