Mongoose: validation error path is required

后端 未结 5 2033
一整个雨季
一整个雨季 2020-12-09 15:55

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 \'

相关标签:
5条回答
  • 2020-12-09 16:27

    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 });
    
    0 讨论(0)
  • 2020-12-09 16:27

    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.

    0 讨论(0)
  • 2020-12-09 16:40

    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.

    0 讨论(0)
  • 2020-12-09 16:41

    For me, the quick and dirty fix was to remove encType="multipart/form-data" from my input form field.

    Before, <form action="/users/register" method="POST" encType="multipart/form-data">
    and, after <form action="/users/register" method="POST">

    0 讨论(0)
  • 2020-12-09 16:46

    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.

    0 讨论(0)
提交回复
热议问题