【问题标题】:Hide associate model's data in sails在sails中隐藏关联模型的数据
【发布时间】:2020-07-29 08:35:42
【问题描述】:

我有两个模型 Account.jsContent.js

content.js 我有这样的关联帐户模型

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

account.js 模型文件中 我有

    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 },
      },
};

在获取内容时,我会获得还包括帐户详细信息的内容列表。

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

我只想要用户的 fname 和 lname 并隐藏通行证、钱包余额等,我该怎么做呢?

【问题讨论】:

    标签: node.js json model sails.js waterline


    【解决方案1】:

    您必须使用 docs 中提到的 customToJSON。

    用下面这句话来隐藏account.js中不需要的字段:

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

    在模型的属性对象之后添加。

    【讨论】:

    • 有没有办法可以将它们隐藏在 content.js 中?从帐户(蓝图)获取时我可能需要电子邮件、电话
    猜你喜欢
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 2014-07-31
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多