Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
Mongoose Virtuals : async setter

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!