我想过滤来自客户端但在创建新文档时不想要的属性。我试图使用下划线的_.pick(),但似乎我无法覆盖 doc 。
Meteor.Collection.prototype.addTimestamps = function () {
this.deny({
insert: function(userId, doc) {
doc.createdAt = Date.now();
doc.updatedAt = Date.now();
return false;
},
update: function(userId, doc, fieldNames, modifier) {
modifier.$set.updatedAt = Date.now();
return false;
},
});
};
Entries.addTimestamps();
Entries.allowed = ['_id', 'content', 'createdAt', 'updatedAt'];
Entries.allow({
insert: function (userId, doc) {
doc = _(doc).pick(Entries.allowed);
doc.userId = userId;
return !! userId;
},
update: function (userId, doc, fieldNames, modifier) {
return doc.userId === userId;
},
remove: function (userId, doc) {
return doc.userId === userId;
}
});
答案 0 :(得分:1)
在第
行写入doc
doc = _(doc).pick(Entries.allowed);
您正在覆盖doc
变量,以便它不再指向实际的doc
对象。你想要的是改变对象本身。
您需要delete
所有未列入白名单的doc
媒体资源。示例实现:
insert: function(userId, doc) {
var keys = _.keys(doc);
keys = _.difference(keys, Entries.allowed);
_.each(keys, function(key) {
delete doc[key];
});
}