title: How to fix ' ValidationError: users validation failed: name: Path `name` is required.' error in Mongo?
body: <p>I'm creating a new user and checking whether the user email previously exists or not. If not then it is creating a new user and saving it. What do I need to correct this validation error I am getting?</p>
  
  <p>I have already tried by setting name as not required. But it still does not work.</p>
  
  <p>This is my User model.</p>
  
  <pre><code>const mongoose = require("mongoose");
  const Schema = mongoose.Schema;
  
  //Declaring User Model
  const UserSchema = new Schema({
    name: {
      type: String,
      required: true
    },
    password: {
      type: String,
      required: true
    },
    email: {
      type: String,
      required: true
    },
    avatar: {
      type: String
    },
    date: {
      type: Date,
      default: Date.now
    }
  });
  
  module.exports = User = mongoose.model("users", UserSchema);
  </code></pre>
  
  <p>This is the user route.</p>
  
  <pre><code>router.post("/register", (req, res) =&gt; {
    User.findOne({ email: req.body.email }).then(user =&gt; {
      if (user) {
        return res.status(400).json({ email: "email already exists" });
      } else {
        const avatar = gravatar.url(req.body.email, {
          s: "200", //size
          r: "pg", //rating
          d: "mm" //default
        });
        const newUser = new User();
        const newUser = {
          name: req.body.name,
          email: req.body.email,
          avatar,
          password: req.body.password
        };
        bcrypt.genSalt(10, (err, salt) =&gt; {
          bcrypt.hash(newUser.password, salt, (err, hash) =&gt; {
            if (err) throw err;
            newUser.password = hash;
            newUser
              .save()
              .then(user =&gt; res.json(user))
              .catch(err =&gt; console.log(err));
          });
        });
      }
    });
  });
  </code></pre>
  
  <p>I am expecting it to get stored. This is the error I am getting.</p>
  
  <pre><code>{ ValidationError: users validation failed: name: Path `name` is required.
      at ValidationError.inspect (G:\devconnector\node_modules\mongoose\lib\error\validation.js:59:24)
      at formatValue (internal/util/inspect.js:493:31)
      at inspect (internal/util/inspect.js:191:10)
      at Object.formatWithOptions (util.js:84:12)
      at Console.(anonymous function) (console.js:191:15)
      at Console.log (console.js:202:31)
      at newUser.save.then.catch.err (G:\devconnector\routes\api\users.js:39:35)
      at process._tickCallback (internal/process/next_tick.js:68:7)
    errors:
     { name:
        { ValidatorError: Path `name` is required.
            at new ValidatorError (G:\devconnector\node_modules\mongoose\lib\error\validator.js:29:11)
            at validate (G:\devconnector\node_modules\mongoose\lib\schematype.js:1034:13)
            at G:\devconnector\node_modules\mongoose\lib\schematype.js:1088:11
            at Array.forEach (&lt;anonymous&gt;)
            at SchemaString.SchemaType.doValidate (G:\devconnector\node_modules\mongoose\lib\schematype.js:1043:14)
            at G:\devconnector\node_modules\mongoose\lib\document.js:2133:9
            at process._tickCallback (internal/process/next_tick.js:61:11)
            message: 'Path `name` is required.',
            name: 'ValidatorError',
            properties: [Object],
            kind: 'required',
            path: 'name',
            value: undefined,
            reason: undefined,
            [Symbol(mongoose:validatorError)]: true } },
            _message: 'users validation failed',
            name: 'ValidationError' }
  </code></pre>