0

I've seen this question asked before but I'm doing something else so the responses don't seem to be helping. Specific to my situation, is that I have a User Schema and I want to append a Profile Schema to it. I'm trying to instantiate an additional Schema, and append it to an existing one. The single Schema works just fine. So I can't figure out why the error is being thrown.

Everything runs until I add in the Profile Schema. Yarn Start once the Profile Schema is added fails with the following error...

throw new mongoose.Error.OverwriteModelError(name); ^ OverwriteModelError: Cannot overwrite user model once compiled. at new OverwriteModelError (C:\Users\Anthony Trejo\Documents\Coding Projects\wave\node_modules\mongoose\lib\error\overwriteModel.js:18:11) at Mongoose.model (C:\Users\Anthony Trejo\Documents\Coding Projects\wave\node_modules\mongoose\lib\index.js:355:13) at Object. (C:\Users\Anthony Trejo\Documents\Coding Projects\wave\models\User.js:29:34) at Module._compile (module.js:649:30) at Object.Module._extensions..js (module.js:660:10) at Module.load (module.js:561:32) at tryModuleLoad (module.js:501:12) at Function.Module._load (module.js:493:3) at Module.require (module.js:593:17) at require (internal/module.js:11:18) at Object. (C:\Users\Anthony Trejo\Documents\Coding Projects\wave\routes\api\profile.js:9:14) at Module._compile (module.js:649:30) at Object.Module._extensions..js (module.js:660:10) at Module.load (module.js:561:32) at tryModuleLoad (module.js:501:12) at Function.Module._load (module.js:493:3)

here is the code for User.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// Create Schema
const UserSchema = new Schema({
 name: {
   type: String,
   required: true
 },
 email: {
   type: String,
   required: true
},
password: {
   type: String,
   required: true
 },
avatar: {
  type: String,   
},
date: {
  type: Date,
  default: Date.now
}
});

module.exports = User = mongoose.model('user', UserSchema);

This is the Profile Schema I want to add

const mongoose = require("mongoose");
const Schema = mongoose.Schema;


// Create Schema
const ProfileSchema = new Schema({
    user: {
        type: Schema.Types.ObjectId,
        ref: "User"
    },
    handle: {
       type: String,
    required: true,
    max: 30
},
company: {
    type: String
},
website: {
    type: String
},
location: {
    type: String,
    required: true
},
role: {
    type: String,
    required: true
},
bio: {
    type: String
},
social: {
    youtube: {
        type: String
    },
    facebook: {
        type: String
    },
    twitter: {
        type: String
    },
    instagram: {
        type: String
    },
    linkedIn: {
        type: String
    }
}
});

module.exports = Profile = mongoose.model('Profile', ProfileSchema);

This is the profile route (don't know if anybody needs this part)

const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");
const passport = require("passport");

// Load Profile Model
const Profile = require("../../models/Profile");
// Load User Model
const User = require("../../models/User");

// GET api/profile/test
// test profile route
// public
router.get("/test", (req, res) => res.json({ msg: "profile works" }));

// GET api/profile
// GET current user profile route
// private
router.get(
  "/",
  passport.authenticate("jwt", { session: false }),
  (req, res) => {
    const errors = {};
    Profile.findOne({ user: req.user.id })
      .then(profile => {
        if (!profile) {
         errors.noprofile = "User Does not Exist.";
          return res.status(404).json();
        }
        res.json(profile);
      })
      .catch(err => res.status(404).json(err));
  }
);

module.exports = router; 
  • The error comes because somewhere in your code you actually have a statement like `mongoose.model('user', UserSchema);` with exactly the same `'user'` model name being registered more than once. That first argument is a "unique" name to register the model as. So in truth you have more code that just what is listed here. I suggest you do a global search in your codebase for the registered model names you have and find out which one you attempted to register twice. – Neil Lunn Jun 07 '18 at 21:35
  • Just a tip. When you post a question here and such an obvious duplicate match is suggested to you, as that is what happens when you type in your question title at the time of posting the question, Read it! And then realize that asking the same thing leads to the same answer. – Neil Lunn Jun 07 '18 at 21:37
  • @NeilLunn So I ran a global search for mongoose.model('user', UserSchema); that code line only comes up once. Yarn Start works if take out Profile.js. The app breaks when it's in the code base. On that file user is referenced in line 7 and 9. I'm not trying to overwrite, I recognized that the other question was similar, however, the circumstances are different and that fix didn't help. It's literally the first sentence of my question. So please give it rest about reading other answers. – Anthony P. Trejo Jun 07 '18 at 23:30
  • We cannot debug your entire application here. You can show a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) which demonstrates otherwise and people would then be happy to look at it. Anything else is a direct duplicate since despite your assertions, unless you can "prove it" by such an example then the most probable cause is exactly what is described. In short, the onus here is on "you" and not on everyone else to prove this. So please alter your question with such a reproducible case or spot your error during the construction of such a case. – Neil Lunn Jun 08 '18 at 00:21

0 Answers0