I have this virtual :
userSchema
.virtual('password')
.set(function setPassword(password) {
this._password = password;
this.salt = uuidv1();
this.encry_password = this.securePassword(password);
})
.get(function getPassword() {
return this._password;
});
Now my problem is that this.securePassword(password); is an async function.
async securePassword(plainpassword) {
if (!plainpassword) return '';
try {
return await argon2.hash(plainpassword);
} catch (err) {
return '';
}
}
Now if i do like this :
userSchema
.virtual('password')
.set(function setPassword(password) {
this._password = password;
this.salt = uuidv1();
this.securePassword(password).then((value) => {
this.encry_password = value;
});
})
.get(function getPassword() {
return this._password;
});
Or like this :
userSchema
.virtual('password')
.set(async function setPassword(password) {
this._password = password;
this.salt = uuidv1();
this.encry_password = await this.securePassword(password)
})
.get(function getPassword() {
return this._password;
});
What i get is this error :
"User validation failed: encry_password: Path `encry_password` is required."
Any solution on how can i solve this possibile with virtuals?