Mongoose findOneAndUpdate and runValidators not working
See original GitHub issueI am having issues trying to get the ‘runValidators’ option to work. My user schema has an email field that has required set to true but each time a new user gets added to the database (using the ‘upsert’ option) and the email field is empty it does not complain:
var userSchema = new mongoose.Schema({
facebookId: {type: Number, required: true},
activated: {type: Boolean, required: true, default: false},
email: {type: String, required: true}
});
findOneAndUpdate code:
model.user.user.findOneAndUpdate(
{facebookId: request.params.facebookId},
{
$setOnInsert: {
facebookId: request.params.facebookId,
email: request.payload.email,
}
},
{upsert: true,
new: true,
runValidators: true,
setDefaultsOnInsert: true
}, function (err, user) {
if (err) {
console.log(err);
return reply(boom.badRequest(authError));
}
return reply(user);
});
I have no idea what I am doing wrong, I just followed the docs: http://mongoosejs.com/docs/validation.html
In the docs is says the following:
Note that in mongoose 4.x, update validators only run on $set and $unset operations. For instance, the below update will succeed, regardless of the value of number.
I replaced the $setOnInsert with $set but had the same result.
Issue Analytics
- State:
- Created 8 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Mongoose findOneAndUpdate and runValidators not working
In the docs is says the following: Note that in mongoose 4.x, update validators only run on $set and $unset operations. For instance,...
Read more >Mongoose findOneAndUpdate and runValidators not working
I am having issues trying to get the 'runValidators' option to work. My post schema has an comments.replies.creator field that has required ...
Read more >Mongoose v6.8.2: Validation
To turn on update validators, set the runValidators option for update() , updateOne() , updateMany() , or findOneAndUpdate() . Be careful: update validators...
Read more >[Solved]-mongoose upsert not working-mongodb
Coding example for the question mongoose upsert not working-mongodb. ... var Hosts = mongoose.model('Hosts', new Schema({ address: String, model: String } ...
Read more >Node.js – Mongoose findOneAndUpdate and runValidators not ...
I am having issues trying to get the 'runValidators' option to work. My user schema has an email field that has required set...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
So the below script:
Gives me a validation error as expected in 4.0.6. Can you run that script locally and double check that it works for you @Jdruwe ?
I created a plugin to validate required model properties before doing
update
operations in mongoose.Must be called explicitly as a method from the model, so I recommend chaining it a
.then
chain prior to the update call.