2014-06-11 2 views
1

내 앱에서 사용자는 자신의 모든 알림을 삭제할 수 있습니다. 이벤트 핸들러로서 나는이 있습니다userId로 레코드 제거 허용되지 않음

Uncaught Error: Not permitted. Untrusted code may only remove documents by ID. [403] 

이 내가 추가하는 것을 잊었다 권한인가, 아니면 일반적으로이 작업을 수행 할 수 없습니다 :

가 호출
Template.clearNotifications.events({ 
    'click .clear-notifications': function() { 
    Notifications.remove({userId: Meteor.user()._id}); 
    } 
}); 

,이 오류가 발생합니다 ? 그렇다면 사용자 알림을 삭제할 수있는 옵션은 무엇입니까?

var notification = Notifications.findOne({userId:Meteor.userId()}); 
Notifications.remove({_id:notification._id}) 

설명 : 클라이언트에

The behavior of remove differs depending on whether it is called by trusted or untrusted code. Trusted code includes server code and method code. Untrusted code includes client-side code such as event handlers and a browser's JavaScript console.

Trusted code can use an arbitrary Mongo selector to find the documents to remove, and can remove more than one document at once by passing a selector that matches multiple documents. It bypasses any access control rules set up by allow and deny. The number of removed documents will be returned from remove if you don't pass a callback.

As a safety measure, if selector is omitted (or is undefined), no documents will be removed. Set selector to {} if you really want to remove all documents from your collection.

Untrusted code can only remove a single document at a time, specified by its _id. The document is removed only after checking any applicable allow and deny rules. The number of removed documents will be returned to the callback.

http://docs.meteor.com/#remove

답변

1

는, 유성은 허용이 문서의 _ID 을 통과 할 때 신뢰할 수없는 영역 (클라이언트)에서 제거

+0

감사합니다. Dave, 이것이 내가 찾고있는 것입니다. 그리고 클라이언트에서 제거하면 게시 된 것들만 제거된다는 것을 깨닫지 못했습니다! – John

0

에만 작동 ID로 문서를 제거합니다. 다행히 쉬운 솔루션이있다 - 단지 각각을 현재 사용자의 모든 통지를 반복 제거 :

Template.clearNotifications.events({ 
    'click .clear-notifications': function() { 
    Notifications 
     .find({userId: Meteor.userId()}) 
     .forEach(function(notification) { 
     Notifications.remove(notification._id); 
     }); 
    } 
}); 

이는 클라이언트가 알고 통지 (게시 된 것들)을 제거합니다 있음을 유의하십시오. 제거해야 할 데이터베이스에 추가 알림이있는 경우 (가장 최근의 10 개의 문서 만 게시 한 경우 일 수도 있음) method을 사용해야합니다. 예를 들어 :

Meteor.methods({ 
    removeAllNotifications: function() { 
    Notifications.remove({userId: this.userId}); 
    } 
}); 

당신과 함께 클라이언트에서 호출 할 수있는 :

Meteor.call('removeAllNotifications'); 
+0

OK,하지만 복수의 통지가있는 경우, 내가 그들을 어떻게 무엇을 반복합니까? – John

+0

신뢰할 수있는 코드 (서버 측)가이를 수행 할 수 있습니다. 임의의 Mongo 선택자에 의해 선택된 통지를 제거하는 서버 측에서 메소드를 생성합니다. –