Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda