mon*_*onk 8 javascript collections meteor
我是Learnign Meteor并遇到了这种情况,我正在关注tuts plus的Meteor教程.视频中的代码与集合更新完全相同,但在我的浏览器中显示此错误:
未捕获错误:不允许.不受信任的代码只能按ID更新文档.[403]
代码在这里:
Template.person.events({
'click': function (e, t) {
Session.set("edit-"+ t.data._id, true);
},
'keypress input': function(e,t){
if(e.keyCode === 13){
var docid = Session.get("edit-"+ this._id);
People.update(t.data, {$set: {name: e.currentTarget.value}});
Session.set("edit-"+ t.data._id, false);
}
}
});
Run Code Online (Sandbox Code Playgroud)
Aks*_*hat 16
对于在客户端/浏览器端运行的代码,您只能使用_id字段作为查询.在服务器上,您可以随意运行它.
修改代码,以便先获取文档,然后使用它_id来执行更新.
var person = People.findOne(t.data);
People.update({_id: person._id}, {$set: {name: e.currentTarget.value}});
Run Code Online (Sandbox Code Playgroud)
我假设t.data是某种查询?如果它_id尝试使用{_id: t.data作为查询而不是.无论哪种方式,只要选择器update 只使用_id 它应该没问题.
这可能适用于您所关注的教程的原因是此更改最近被引入以锁定安全性.