Specify returned fields in Node.js / Waterline?

自闭症网瘾萝莉.ら 提交于 2019-11-30 13:43:57

So actually found a weird workaround for this. the fields param WILL work as long as you pass other params with it such as limit or order:

User.find({}, {fields: {username:1}}).limit(1);

Note that this will NOT work with findOne or any of the singular returning types. This means in your result callback you will need to do user[1].

Of course the other option is to just scrub your data on the way out, which is a pain if you are using a large list of items. So if anything this works for large lists where you might actually set limit(20) and for single items you can just explicitly return paras until select() is available.

This is an update to the question, fields is no longer used in sails 11, please use select instead of fields.

Model.find({field: 'value'}, {select: ['id', 'name']})
  .paginate({page: 1}, {limit: 10})
  .exec(function(err, results) {
    if(err) {
      res.badRequest('reason');
    }
    res.json(results);
});

Waterline does not currently support any "select" syntax; it always returns all fields for a model. It's currently in development and may make it into the next release, but for now the best way to do what you want would be to use model class methods to make custom finders. For example, User.findUser(criteria, cb) could find a user give criteria, and then check whether it was the logged-in user before deciding which data to return in the callback.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!