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;