Reputation: 41
I am use NodeJS and mongodb for database and mongoose
[I am provided value for name and email but it still shows that email and name required]
Here is my code
const mongoose = require('mongoose')
const validator = require('validator')
const User = mongoose.model('User', {
name: {
type: String,
required: true,
trim: true,
tolowercase: true
},
email: {
type: String,
required: true,
trim: true,
lowercase: true,
validate(value){
if(!validator.isEmail(value)){
throw new Error('Invalid Email')
}
}
},
password: {
type: String,
minlength: 7,
//required: true,
trim: true,
validate(value){
if(value.toLowerCase().includes('password')){
throw new Error('Password can\'t contains word password')
}
}
},
age: {
type: Number,
default: 0
}
})
If I comment all required statement in name and email then also it will take only age and save it into db
Upvotes: 1
Views: 3171
Reputation: 279
It's most probably happening because of the Mismatching of Content-Type, what you are sending from the postman and what you are receiving in your node app. Please check if you are using body-parser and parsing the data in JSON like: app.use(bodyParser.json()); and not like: app.use(bodyParser.urlencoded({ extended: false })); As I think you must be sending a header with Content-Type: application/json from the postman and receiving it as urlencoded or vice-versa.
Upvotes: 1
Reputation: 695
The "path" part in the error response could indicate that the parameter belongs in the URL path not the body.
Upvotes: 0