Looking for a way to remove object properties before sending them to the front-end.
Is there any reason why this is working:
var obj = {
name: 'cris',
age: 22,
}
console.log(obj) //output name, age
delete obj.name
console.log(obj) //output age
and this isn't:
User.findOne({ username: req.query.username }, function (err, user) {
if (user != null) {
console.log(user) //output all props
delete user.salt || delete user['salt']
console.log(user) //output all props
}
});
user
is a Mongoose document and not a regular object.
You can convert it to one using toObject()
:
user = user.toObject();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With