I have a user schema with unique: true
and the following validator:
{ // checks if email already used
validator: async (value) => {
let user = await mongoose.model('User').findOne({ email: value });
return !user;
},
message: 'Email already exists'
}
It works as expected when creating a new user, the problem is with updating.
I'm using doc.save()
to update a user documents (it's a valid way to update documents according to Mongoose documentation). but that validator is being triggered again.
Is there a way to use the save
methods without triggering that validator again?
Thanks.