Petar Vasilev
Petar Vasilev

Reputation: 4735

SailsJS returning all data

I have User model in my Sails app which of course has password. So when I create a new user Sails responds with json containing all the data of the newly created user including the hashed password. Is there a way to prevent Sails from outputting certain fields like the password?

Upvotes: 0

Views: 131

Answers (2)

Petar Vasilev
Petar Vasilev

Reputation: 4735

Add in the model:

afterCreate: function(attrs, next) {
    delete attrs.password;
    next();
}

Upvotes: 1

dave
dave

Reputation: 64657

Yes. You just need to override the toJson function for the model, like so:

module.exports = {
  attributes: {
  name: 'string',
  password: 'string',
  // Override toJSON instance method
  // to remove password value
  toJSON: function() {
    var obj = this.toObject();
    delete obj.password;
    return obj;
  }
 }
}

Upvotes: 6

Related Questions