Reputation: 31
Here is my schema:
const personSchema = new mongoose.Schema({
name: {
type: String,
minlength: 3,
unique: true,
required: true
},
number: {
type: String,
minlength: 8,
required: true
}
})
My update code:
app.put('/api/persons/:id', (request, response, next) => {
const body = request.body
const person = {
name: body.name,
number: body.number
}
let opts = {
runValidators: true
};
Person.findByIdAndUpdate(request.params.id, person, { new: true }, opts)
.then(updatedPerson => {
response.json(updatedPerson)
})
.catch(error => next(error))})
I try to update the information, and the validation does not work with update, it works when create a new Person. I know Mongoose Update Validator are off by default, I have tried many ways to enable it, however, it does not work, could anyone help me, please ?
Upvotes: 1
Views: 206
Reputation: 21
place your options in the third params:
Person.findByIdAndUpdate(request.params.id, person, { new: true, runValidators: true })
Upvotes: 1