You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, when running update validators, the document being updated may not be in the server's memory, so by default the value of this is not defined.
If I understand correctly now Mongoose always uses context = 'query'.
So wouldn't be the following statement more accurate:
However, when running update validators, this refers to the query object instead of the document.
Also then an additional example could be added:
/** * Since Mongoose always uses `context = 'query'` when running update validators `this` refers to the query object! */it('Update validation with query object',function(done){// acquit:ignore:startconsttoySchema=newSchema({color: String,name: String});// acquit:ignore:endtoySchema.path('color').validate(function(value){// When running update validators `this` refers to the query object.if(this.getUpdate().$set.name.toLowerCase().indexOf('red')!==-1){returnvalue==='red';}returntrue;});constToy=db.model('ActionFigure',toySchema);constupdate={color: 'blue',name: 'Red Power Ranger'};constopts={runValidators: true};Toy.updateOne({},update,opts,function(error){assert.ok(error.errors['color']);// acquit:ignore:startdone();// acquit:ignore:end});});
If my conclusion is right I can make a PR with the mentioned changes.
Thanks in advance!!
The text was updated successfully, but these errors were encountered:
hasezoey
added
the
docs
This issue is due to a mistake or omission in the mongoosejs.com documentation
label
Nov 8, 2022
Prerequisites
Issue
In the validation docs you mention that:
If I understand correctly now Mongoose always uses
context = 'query'
.So wouldn't be the following statement more accurate:
Also then an additional example could be added:
If my conclusion is right I can make a PR with the mentioned changes.
Thanks in advance!!
The text was updated successfully, but these errors were encountered: