Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose: validation error path is required

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?

like image 596
2trill2spill Avatar asked Jul 27 '15 21:07

2trill2spill


2 Answers

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 }); 
like image 171
Richard Christensen Avatar answered Sep 18 '22 12:09

Richard Christensen


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.

like image 40
Parikshit Hooda Avatar answered Sep 17 '22 12:09

Parikshit Hooda