javascript - Waterline/nodejs 有没有办法不返回所有字段?或者我应该过滤json?

标签 javascript json node.js waterline

我正在使用nodejs、express 和waterline 编写一个小REST API。如果我检索一条记录并使用 res.json(post); 返回它,则其中包含我不想在 API 中显示的数据(例如 ID、关联等)。

有没有办法告诉水线要检索哪些字段?如果没有,将发送的内容列入白名单的最佳方法是什么?

最佳答案

水线

从水线 Angular 来看,您可以在检索记录时使用select,例如 docs :

// Returns only the field name
Model.find({ where: { age: { '<': 30 } }, select: ['name'] })

您还可以实现toJSON正如@Rodrigo 所建议的。

node.js/lodash

从node.js方面,您还可以使用lodash的pick(),例如docs :

var object = { 'user': 'fred', 'age': 40 };
_.pick(object, 'user');
// → { 'user': 'fred' }

关于javascript - Waterline/nodejs 有没有办法不返回所有字段?或者我应该过滤json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31032947/

相关文章:

javascript - 从 Ajax 加载的内容加载内容

javascript - 使用航路点时的最短路线

arrays - 具有多个数组的 PowerShell ConvertTo-JSON

javascript - 使用 jQuery 处理 JSON 对象

javascript - 为什么我的 node.js 得到响应被 chop

javascript - Modernizr 调用后 $(window).load()...不工作

javascript - 使用 websocket 的 gRPC 替代方案?

java.sql.TimeStamp 没有默认构造函数

javascript - 从 json 返回 html 标签,而不在express中转换为ascii

node.js - 将流写入缓冲区对象