Can't fix mongoose override model error

I am using an angular-fullstack generator and I have added a human model. When I try to require the human 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 as for the "Thing" model that comes with the generator. Also searched and where Person is in the codebase and only in 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?

+3


source to share


1 answer


As I understand from the information you posted, the problem appears to be caused by the following lines:

in the object. (/Users/dev/wishlist/server/api/wishList/person.model.js:11:27)

module.exports = mongoose.model('Person', PersonSchema);

      

in the object. (/Users/dev/wishlist/server/api/wishList/wishList.controller.js:4:14)

var Person = require('./person.model');

      

This error most often occurs due to a mismatch between Mongoose models.



Somewhere along the way, you have defined this model under a different name, but with the same schema.

To verify that this is what is causing your error, add this code in person.model.js

right after the request mongoose

:

'use strict';

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

mongoose.models = {};
mongoose.modelSchemas = {};

      

This will clear your mongoose data and free up all existing models and schemas.

I found the above information on the following LINK .

I also found some additional discussion about this issue HERE and HERE .

+4


source







All Articles