在Node.js / Waterline中指定返回的字段? [英] Specify returned fields in Node.js / Waterline?

查看:180
本文介绍了在Node.js / Waterline中指定返回的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个请求:

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

我知道我可以使用 toJSON 但是我不喜欢这种方法,因为有时我需要不同的参数。例如,如果是登录用户,我将返回其电子邮件和其他参数。但是,如果是请求堡,他的数据是由不同的用户进行的,它不会包含电子邮件和一小部分参数。

I know I can use toJSON in the model however I don't like this approach since sometimes I need different parameters. For instance if it's the logged in user I will return their email and other parameters. However if it the request fort he same data is made by a different user it would not include the email and a smaller subset of parameters.

我也试过使用:

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

但没有任何运气。如何指定我需要返回的字段?

But not having any luck. How can I specify the fields I need returned?

推荐答案

所以实际上找到了一个奇怪的解决方法。只要您通过其他参数(例如 limit 字段订单

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);

请注意,这不适用于findOne或任何单个返回类型。这意味着在您的结果回调中,您将需要执行用户[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].

当然另一个选项是在您出路的时候擦除数据,这是一个如果您正在使用大量物品,则会产生痛苦。所以如果有什么事情适用于大型列表,你可能实际设置 limit(20),而对于单个项目,你可以直接返回到$ code> select() 可用。

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.

这篇关于在Node.js / Waterline中指定返回的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆