Failed to save document in Mongoose. Ruler check error for path error

Below is my diagram:

var userSchema = new Schema({ username: { type: String, required: true }, password: { type: String, required: false } }); 

Now, when I try to save the document of the above schema, I get the following error:

 { message: 'Validation failed', name: 'ValidationError', errors: { username: { message: 'Validator "required" failed for path username', name: 'ValidatorError', path: 'username', type: 'required' } } } 

The above error object is returned by mongoose on save. I was looking for this error, but could not understand what was wrong. The document I'm trying to save is as follows:

 { username: "foo" password: "bar" } 

Any idea what that means? I also searched mongoose docs but couldn't find anything in the validation section.

+4
source share
1 answer

Firstly, you are missing the comma (,) after foo .

Now, { username: "foo", password: "bar" } JSON sent via http, is our actual object in your server code?

If so, try console.log(youVariable.username) and see if it shows undefined or the value foo . If you see undefined then your object will not be parsed correctly.

You can make sure that someone sends a POST request, sends to the "application / json" header, you can receive something else, so your JSON is not processed by a valid javascript object.

+1
source

All Articles