And*_*luZ 2 node.js sails.js waterline
我有这样的模型:
module.exports = {
attributes: {
email: {
type: 'email',
required: true,
unique: true
},
password: {
type: 'string',
minLength: 6,
required: true
},
articles: {
collection: ‘article',
via: 'owners'
},
toJSON: function() {
var obj = this.toObject();
delete obj.password;
return obj;
}
}
}Run Code Online (Sandbox Code Playgroud)
如何在不删除toJSON函数中的其他字段的情况下仅查询"email"字段?
这个问题的原因是,如果我有数千篇带有嵌入图像的文章等等,收集所有这些文章并将其删除到toJSON函数中只是为了显示"电子邮件"字段,这将是一种浪费.
因此,必须有更好的方法来实现这一目标.我希望你们能回答我这个问题:)
更新 我忘了说我尽可能使用blueprint.js以避免在控制器中覆盖创建,删除,更新......
它没有很好的文档记录,但Waterline提供了一个 在此实施的选择标准https://github.com/balderdashy/waterline-criteria/blob/master/lib/projections/select.js
我测试了以下内容并且有效.{}可能是任何查询:
Model.find({}, {select: ['email']}).exec(function(err, result) {
return res.send(result);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
72 次 |
| 最近记录: |