I am trying to remove the password from the JSON serialized User model.
For some reason my controller returning res.json({user: myUser});
is returning the full user including the password. Below is my User model. Thoughts?
# models/User.js
var User = {
attributes: {
username: 'string',
password: 'string'
},
// Override toJSON method to remove password from API
toJSON: function() {
var obj = this.toObject();
// BELOW NOT WORKING
delete obj.password;
return obj;
}
};
module.exports = User;
You are adding toJSON
as a class method (outside of the attributes
object); it needs to be an instance method:
# models/User.js
var User = {
attributes: {
username: 'string',
password: 'string',
// Override toJSON method to remove password from API
toJSON: function() {
var obj = this.toObject();
delete obj.password;
return obj;
}
}
};
module.exports = User;
This will add the toJSON
method to every instance of User
, and will work as you expect when doing things like res.json()
.
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