2016-05-11 52 views
0

我試圖刪除集合中的數據,但是,我得到remove failed: Access denied。我不確定我做錯了什麼。刪除與用戶相關的集合中的所有數據

DB - Files.find

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

路徑:file.js

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

回答

0

你應該考慮到流星方法這一點。

服務器端代碼:

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

你應該考慮在控制檯輸入這些命令刪除不安全和自動發佈軟件包:

meteor remove autopublish 
meteor remove insecure 

下面有一個如何發佈一個例子文件集合(這裏您還可以添加安全功能,例如僅查找和發佈屬於具有正確標識的用戶的文件):

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'); 

閱讀,發佈,並在流星文檔訂閱了。鏈接:http://guide.meteor.com/methods.htmlhttps://www.meteor.com/tutorials/blaze/security-with-methodshttps://www.meteor.com/tutorials/blaze/publish-and-subscribe

1

如果你卸載自動發佈包使用方法如上回答:

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);

相關問題