kumar sanket
kumar sanket

Reputation: 35

I am trying to create a user model but i am getting a MissingSchemaError

I am trying the create a user model in Node.js and Mongoose, but I am getting an error:

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

I have tried to fix this using the help of the following sites, but it is not working for me.

How to register and call a schema in mongoose

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

User Model

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

How I can create this model?

Upvotes: 0

Views: 579

Answers (2)

Nikoloz Bokeria
Nikoloz Bokeria

Reputation: 51

Actually, you didn't pass the Schema in the Model, you pass the String "UserSchema" you should pass the UserSchema

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

and require

const {User} = require(PATH TO MODEL)

Upvotes: 1

shili.oussama
shili.oussama

Reputation: 44

Try changing user to User with uppercase when registering your model and remove the User after module.exports like this:

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

Upvotes: 0

Related Questions