Hide associate model's data in sails

76 Views Asked by At

I have two model Account.js and Content.js

In content.js i have associated Account model like this

module.exports = {
  attributes: {
    title: { type: "string" },
    description: { type: "string" },
    userId: { model: "account", required: true },
    categoryId: { model: "contentcategory" },
  },
}; 

In account.js model file i have

    module.exports = {
      attributes: {
        fName: { type: "string" },
        lName: { type: "string" },
        pass: { type: "string", required: true },
        email: { type: "string", required: true, isEmail: true, unique: true },
        phone: { type: "number" },
        walletBalance: { type: "number", defaultsTo: 0 },
      },
};

When fetching the a content I get list of content that also includes account details.

{
    title:"category Name",
    description : "CAtegort Desc",
    userId : {
    id: 12552,
    fName:John,
    lName:Doe,
    pass : "23623562356",
    email:"john Doe",
    phone: "124151516",
    walletBalance: 234
    }
}

I want only the fname and lname of the user and hide pass, walletbalance etc, how can i do it sails waterline

1

There are 1 best solutions below

1
Jordi On

You have to use customToJSON as mentioned in the docs.

with the following sentence to hide unwanted fields in account.js:

customToJSON: function () {
 return _.omit(this, ['pass', 'email', 'phone', 'walletBalance'])
},

Add it after model's attribute object.