【问题标题】:I am trying to create a user model but i am getting a MissingSchemaError我正在尝试创建用户模型,但出现 MissingSchemaError
【发布时间】:2019-05-07 20:52:55
【问题描述】:

我正在尝试在 Node.js 和 Mongoose 中创建用户模型,但出现错误:

MissingSchemaError : Schema hasn't been registered for the model "user"

我已尝试使用以下网站的帮助来解决此问题,但它不适合我。

How to register and call a schema in mongoose

https://github.com/Automattic/mongoose/issues/3105

用户模型

const mongoose = require("mongoose");

const UserSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true,
    unique: true
  },
  password: {
    type: String,
    required: true
  },
  avatar: {
    type: String
  },
  status: {
    type: Boolean,
    default: false
  },
  date: {
    type: Date,
    default: Date.now
  }
});

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

user.js

const express = require("express");
const gravatar = require("gravatar");
const bcrypt = require("bcryptjs");
const router = express.Router();
const { check, validationResult } = require("express-validator/check");
// const User = require("../../models/User");

// @route POST api/users
// @desc Register user
// @access Public

    enter code here`
router.post(
  "/",
  [
    check("name", "Name is required")
      .not()
      .isEmpty(),
    check("email", "Please include valid Email").isEmail(),
    check(
      "password",
      "Please enter a password with 6 or more characters"
    ).isLength({
      min: 6
    })
  ],
  async (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      console.log(errors);
      return res.status(400).json({ errors: errors.array() });
    }

    const { name, email, password } = req.body;

    try {
      let user = await User.findOne({ email });
      if (user) {
        res.status(400).json({
          errors: [{ msg: "User Already Exists" }]
        });
      }

      const avatar = gravatar.url(email, {
        s: "200",
        r: "pg",
        d: "mm"
      });

      user = new User({
        name,
        email,
        avatar,
        password
      });

      const salt = await bcrypt.genSalt(10);

      user.password = await bcrypt.hash(password, salt);

      await user.save();

      res.send("User Route");
    } catch (err) {
      console.error(err.message);
      res.status(500).send("Sever Error");
    }
  }
);

我怎样才能创建这个模型?

【问题讨论】:

  • 你已经评论了你的用户模型。

标签: node.js mongoose-schema


【解决方案1】:

其实你并没有传递Model中的Schema,你传递的是String "UserSchema" 你应该传递的是UserSchema

const User = mongoose.model("user", UserSchema); 
module.exports = {User}

并且需要

const {User} = require(PATH TO MODEL)

【讨论】:

    【解决方案2】:

    在注册您的模型时尝试将用户更改为大写用户,并删除module.exports 之后的User,如下所示:

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

    【讨论】:

    • user.js 中我已评论的第六行,如果我取消评论,则不需要评论我再次收到错误
    猜你喜欢
    • 2022-07-24
    • 2021-04-30
    • 2019-10-30
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 2019-06-27
    相关资源
    最近更新 更多