简体   繁体   English

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

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

I'm using restapi in sailsjs , I have a user model: 我在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'
    } 

  },
};

I would like to expose some rest api , but I only allow findOne method , and what's more : 我想公开一些rest api,但是我只允许使用findOne方法,而且还有:

I DON'T want the result contains id_card , because it's kind of private info. 我不希望结果包含id_card ,因为它是一种私人信息。

Sailsjs doesn't has beforeFindOne or afterFindOne. Sailsjs没有beforeFindOne或afterFindOne。

what can I do? 我能做什么?

Besides: 除了:

I would like to expose a rest api , such as update. 我想公开一个rest api,例如update。 But I only want rest api to just allow update phone & email, rather than real_name & is_verify . 但是我只希望rest api只允许更新电话和电子邮件,而不是real_name和is_verify。

I can do it in beforeupdate method to limit the update filed. 我可以在beforeupdate方法中执行此操作以限制更新。

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

But these lines of code would NOT be elegant. 但是这些代码行并不完美。 Some may rather write their own api to override it. 有些人可能宁愿编写自己的api来覆盖它。

So, Would it be properly to write my own api to override the rest api in this two situations? 因此,在这两种情况下编写我自己的api以覆盖其余api是否合适?

For your first question: 对于第一个问题:

I DON'T want the result contains id_card , because it's kind of private info. 我不希望结果包含id_card,因为它是一种私人信息。

You should simply add the option protected: true to the model attribute, as in: 您只需向模型属性添加protected: true选项,如下所示:

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

For your second question: I don't really know a way to protect an attribute to update rather than the beforeUpdate you are already using. 对于您的第二个问题:我真的不知道一种保护属性进行更新的方法,而不是您已经在使用的beforeUpdate。

PD: you should probably create different threads for each question you have. PD:您可能应该为每个问题创建不同的主题。

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

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