如何删除 Meteor 中的文档。我收到此错误
"Uncaught Error: Not permitted. Untrusted code may only remove documents by ID. [403]"
列表.js
Template.KullaniciListele.events({
'click #delete': function (id, template) {
Meteor.users.remove({_id:id});
}
});
用户.html
{{#each kullaniciList}}
<tr>
<td><a href="javascript:;" id="delete" >delete</a>
</tr>
{/each}}
最佳答案
Event callbacks采取event
对象作为它们的第一个参数。所以你的代码正在这样做:
Meteor.users.remove({_id: event});
因为event
不是字符串,您会收到错误。假设 kullaniciList
中的每一项包含 _id
(他们是用户吗?),那么您的代码可能如下所示:
Template.KullaniciListele.events({
'click .delete': function (event) {
event.preventDefault();
Meteor.users.remove(this._id);
}
});
请注意,您只能拥有一个 #delete
id 在页面上,所以我将其切换到一个类。模板中的相应行应该是:
<td><a href="#" class="delete">delete</a></td>
请注意,您还错过了结束语 </td>
.
如果您已删除 insecure
包,您需要添加 allow rule或者写一个method以便用户删除真正成功。只是为了看看有什么作用,你可以这样做:
if (Meteor.isServer) {
Meteor.users.allow({
remove: function(userId, doc) {
// JUST FOR TESTING - NEVER DO THIS IN PRODUCTION
return true;
}
});
}
警告!!这只是为了让某些东西正常工作 - 请不要将其添加到生产代码中,因为它允许任何用户删除任何其他用户。要正确执行此操作,您需要确保调用者是管理员或其他人员,但这超出了本问题的范围。
关于javascript - 如何删除 Meteor 中的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31706319/