Specify returned fields in Node.js / Waterline?

前端 未结 3 1625
灰色年华
灰色年华 2021-01-02 13:16

I want to make a request like:

User.find().exec(function(){});

I know I can use toJSON in the model however I don\'t like this

3条回答
  •  轮回少年
    2021-01-02 13: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.

提交回复
热议问题