无法覆盖用户'在将第二个模式添加到数据库时,模型曾一度卷曲。

时间:2018-06-07 21:23:48

标签: node.js mongoose

我之前已经看过这个问题,但我做了其他事情,所以回复似乎没有帮助。具体到我的情况,是我有一个用户架构,我想附加一个配置文件架构。我试图实例化一个额外的Schema,并将其附加到现有的Schema。单个Schema工作得很好。所以我无法弄清楚错误被抛出的原因。

在我添加配置文件架构之前,一切都会运行。添加配置文件架构后的纱线启动失败,并显示以下错误...

  

抛出新的mongoose.Error.OverwriteModelError(name);         ^   OverwriteModelError:编译后无法覆盖user模型。       at new OverwriteModelError(C:\ Users \ Anthony Trejo \ Documents \ Coding Projects \ wave \ node_modules \ mongoose \ lib \ error \ overwriteModel.js:18:11)       在Mongoose.model(C:\ Users \ Anthony Trejo \ Documents \ Coding Projects \ wave \ node_modules \ mongoose \ lib \ index.js:355:13)       在对象。 (C:\ Users \ Anthony Trejo \ Documents \ Coding Projects \ wave \ models \ User.js:29:34)       在Module._compile(module.js:649:30)       at Object.Module._extensions..js(module.js:660:10)       在Module.load(module.js:561:32)       在tryModuleLoad(module.js:501:12)       在Function.Module._load(module.js:493:3)       在Module.require(module.js:593:17)       at require(internal / module.js:11:18)       在对象。 (C:\ Users \ Anthony Trejo \ Documents \ Coding Projects \ wave \ routes \ api \ profile.js:9:14)       在Module._compile(module.js:649:30)       at Object.Module._extensions..js(module.js:660:10)       在Module.load(module.js:561:32)       在tryModuleLoad(module.js:501:12)       在Function.Module._load(module.js:493:3)

这是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);

这是我要添加的配置文件模式

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);

这是个人档案路线(不知道是否有人需要此部分)

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; 

0 个答案:

没有答案