Mongoose: validation error path is required

validationerror: user validation failed: password: path `password` is required
mongoose required not working
mongoose error handling
validationerror user validation failed: name: path name is required
mongoose required default
mongoose enum validation
mongoose email validation
mongoose-unique-validator

I'm trying to save a new document in mongodb with mongoose, but I am getting ValidationError: Path 'email' is required., Path 'passwordHash' is required., Path 'username' is required. even though I am supplying email, passwordHash and username.

Here is the user schema.

    var userSchema = new schema({
      _id: Number,
      username: { type: String, required: true, unique: true },
      passwordHash: { type: String, required: true },
      email: { type: String, required: true },
      admin: Boolean,
      createdAt: Date,
      updatedAt: Date,
      accountType: String
    });

This is how I am creating and saving the user object.

    var newUser = new user({

      /* We will set the username, email and password field to null because they will be set later. */
      username: null,
      passwordHash: null,
      email: null,
      admin: false

    }, { _id: false });

    /* Save the new user. */
    newUser.save(function(err) {
    if(err) {
      console.log("Can't create new user: %s", err);

    } else {
     /* We succesfully saved the new user, so let's send back the user id. */

    }
  });

So why does mongoose return a validation error, can I not use null as temporary value?

In response to your last comment.

You are correct that null is a value type, but null types are a way of telling the interpreter that it has no value. therefore, you must set the values to any non-null value or you get the error. in your case set those values to empty Strings. i.e.

var newUser = new user({

  /* We will set the username, email and password field to null because they will be set later. */
  username: '',
  passwordHash: '',
  email: '',
  admin: false

}, { _id: false });

ValidationError, even if required fields are provided � Issue #5786 , It throws ValidationError, even if the required fie ValidationError here // " Validation failed: content: Path `content` is required., title: Path `title` is required." } Please mention your node.js, mongoose and MongoDB version. In the above examples, you learned about document validation. Mongoose also supports validation for update(), updateOne(), updateMany(), and findOneAndUpdate() operations. Update validators are off by default - you need to specify the runValidators option.

Well, the following way is how I got rid of the errors. I had the following schema:

var userSchema = new Schema({
    name: {
        type: String,
        required: 'Please enter your name',
        trim: true
    },
    email: {
        type: String,
        unique:true,
        required: 'Please enter your email',
        trim: true,
        lowercase:true,
        validate: [{ validator: value => isEmail(value), msg: 'Invalid email.' }]
    },
    password: {
        type: String/
        required: true
    },
    // gender: {
    //     type: String
    // },
    resetPasswordToken:String,
    resetPasswordExpires:Date,
});

and my terminal throw me the following log and then went into infinite reload on calling my register function:

(node:6676) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ValidationError: password: Path password is required., email: Invalid email.

(node:6676) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

So, as it said Path 'password' is required, I commented the required:true line out of my model and validate:email line out of my model.

ValidationError: Path `email` is required. � Issue #509 � linnovate , this error. ValidationError: Path email is required. at model.Document.invalidate (/ app/node_modules/mongoose/lib/document.js:1009:32) Questions: I'm trying to save a new document in mongodb with mongoose, but I am getting Validation

I came across this post when I was looking for resolution for the same problem - validation error even though values were passed into the body. Turns out that I was missing the bodyParser

const bodyParser = require("body-parser")

app.use(bodyParser.urlencoded({ extended: true }));

I did not initially include the bodyParser as it was supposed to be included with the latest version of express. Adding above 2 lines resolved my validation errors.

Mongoose v5.10.0: Validation, validateSync(); assert.equal(error.errors['name'].message, 'Path `name` is The required validator uses the SchemaType's checkRequired() function to� In the above examples, you learned about document validation. Mongoose also supports validation for update() and findOneAndUpdate() operations. In Mongoose 4.x, update validators are off by default - you need to specify the runValidators option. To turn on update validators, set the runValidators option for update() or findOneAndUpdate(). Be

To solve this type of error

ValidationError: Path 'email' is required.

your email is set required in Schema, But no value is given or email field is not added on model.

If your email value may be empty set default value in model or set allow("") in validatior. like

 schemas: {
    notificationSender: Joi.object().keys({
        email: Joi.string().max(50).allow('')
    })
  }

I think will solve this kind of problem.

Mongoose Validation v4.13.20, Whatever queries related to “Path `user` is required mongoose”. ValidationError: Guild validation failed: prefix: Path `prefix` is required. Do you want to request a feature or report a bug? Bug What is the current behavior? Use required validator with some fields, and do findOneAndUpdate with option { runValidators: true} .

Path `user` is required mongoose Code Example, "path": "name" }, "kind": "required", "path": "name" } }, "message": "Product validation failed", "name": "ValidationError" } So i'm using Express Js and Mongoose to� Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

message: path is required - node.js - html, A benefit of using Mongoose when inserting data into MongoDB is its built-in mongo-crud $node index.js Game validation failed: title: Path title is required. @lcswillems someone relying on Mongoose to validate the contents of an HTML <input type="text"> tag in a form could reasonably rely on required to make sure the user actually entered something. Copy link Quote reply

Mongoose Validation Examples – Vegibit, I'm trying to decide how I want to handle validation errors in Mongoose. name: { message: 'Validator "required" failed for path name', name: 'ValidatorError',� It has been working for my own account since I had my twitter user in the database before the email validation was added but I deleted it from mongo and now I can't authenticate via twitter. I like having the email required and @whisher probably took the right path but here is my hack.

Comments
  • Well I don't see where you are setting the newUsers email, passwordHash and username. you are setting them to null then attempting to save it.
  • Can you not do that? null is a value.