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?
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.