繁体   English   中英

我如何限制在sailjs中的REST API结果中指定文件

[英]how can I limit specify filed in rest api result in sailjs

我在sailsjs中使用restapi,我有一个用户模型:

module.exports = {

  schema: true,

  attributes: {

    username : { type: 'string' },    

    real_name : { type: 'string' },

    encrypted_password: {
      type: 'string'
    },

    id_card : { type: 'string' },

    is_verify : { type : 'boolean' } ,

    email : { type: 'string' },

    phone : {
      type: 'string'
    } 

  },
};

我想公开一些rest api,但是我只允许使用findOne方法,而且还有:

我不希望结果包含id_card ,因为它是一种私人信息。

Sailsjs没有beforeFindOne或afterFindOne。

我能做什么?

除了:

我想公开一个rest api,例如update。 但是我只希望rest api只允许更新电话和电子邮件,而不是real_name和is_verify。

我可以在beforeupdate方法中执行此操作以限制更新。

beforeUpdate: function(values, cb) {
    // accessing the function defined above the module.exports
    FilterUpdateField(function() {
      cb();
    })
  }

但是这些代码行并不完美。 有些人可能宁愿编写自己的api来覆盖它。

因此,在这两种情况下编写我自己的api以覆盖其余api是否合适?

对于第一个问题:

我不希望结果包含id_card,因为它是一种私人信息。

您只需向模型属性添加protected: true选项,如下所示:

attributes:{
  id_card : { 
    type: 'string',
    protected: true 
  }, 
}

对于您的第二个问题:我真的不知道一种保护属性进行更新的方法,而不是您已经在使用的beforeUpdate。

PD:您可能应该为每个问题创建不同的主题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM