删除与用户相关的集合中的所有数据

时间:2016-05-11 03:09:18

标签: meteor

我正在尝试删除集合中的数据,但是,我得到了remove failed: Access denied。我不确定我做错了什么。

DB - Files.find

"_id": "",
"url": "",
"userId": "",
"added": ""

路径:file.js

   Template.file.events = {
      "click .delete-photo" : function () {
        Files.remove(this._id);
      }
    };

2 个答案:

答案 0 :(得分:1)

如果你是卸载autopublish包使用方法,如上所述:

Meteor.methods({
 removePhoto: function (photoId) {
 check(photoId, Meteor.Collection.ObjectID);
 Files.remove(photoId);
});

在您的客户端:

Meteor.call("removePhoto", this._id, function(error, affectedDocs) {
  if (error) {
    console.log(error.message);
  } else {
   // Do whatever
   console.log("success");
  }
});

如果您卸载了不安全的软件包,请发布publishand订阅该集合。

Meteor.publish('collectionname',function(){
 return collectionname.find();
}

并订阅: Meteor.subscribe('collectionname);

答案 1 :(得分:0)

您应该考虑将其变成Meteor方法。

服务器端代码:

Meteor.methods({
  removePhoto: function (photoId) {
  check(photoId, Meteor.Collection.ObjectID);
  Files.remove(photoId);
});

您应该考虑通过在控制台中键入以下命令来删除不安全和自动发布的包:

meteor remove autopublish
meteor remove insecure

下面是一个如何发布Files集合的示例(此处您还可以添加安全功能,例如只查找和发布属于具有正确ID的用户的文件):

Meteor.publish('files',function(){
   return Files.find();
}

在您的客户端:

Meteor.call("removePhoto", this._id, function(error, affectedDocs) {
   if (error) {
      console.log(error.message);
   } else {
    // Do whatever
    console.log("success");
   }
});

以下是订阅Files集合的代码:

Meteor.subscribe('collectionname');

阅读Meteor docs上的方法,发布和订阅。链接:http://guide.meteor.com/methods.htmlhttps://www.meteor.com/tutorials/blaze/security-with-methodshttps://www.meteor.com/tutorials/blaze/publish-and-subscribe