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

173
Views
How to always return the executed value of a getter in Mongoose?

I have a mongoose schema with a getter function for one of the properties, for example:

const User = new mongoose.Schema(
    {
        active: Boolean,
        email:  {type: String, get: toLowerCase}
    }
)

and toLowerCase is defined as:

function toLowerCase(str) {
    return str.toLowerCase()
}

When I perform User.findOne(), an example object returned looks like

// user
{
    active: true,
    email: "LowerCase@gmail.com" // note: not lower-cased
}

BUT if access the email property with same object with user.email it returns lowercase@gmail.com

user.email // lowercase@gmail.com

Is there a way to have mongo directly return the executed getter value?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

By default, Mongoose executes getters when converting a document to JSON. That means that email field in the result from User.findOne() will not be in lowercase. But, in the moment when you send your data back with res.json(), getter will be executed and the email field will be set to lowercase value.

about 4 years ago · Juan Pablo Isaza Report
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!