以下是我的場景:當流星訂閱發生變化時,本地收藏中的重複ID
我有一個日曆事件集合。有些事件會附帶一個重複規則。當我訂閱客戶端中的事件時,我正在創建一個名爲LocalEvents
的新本地集合。我循環了從Events
集合發佈的所有事件,並在我的事件每次發生時向我的本地集合中插入一個新文檔。
這裏是我的代碼註釋
//create reactive variable
const searchQuery = new ReactiveVar(null);
export default createContainer(({ params }) => {
//subscribe to subscription passing the reactive var as the variable
const subscription = Meteor.subscribe('Events.publications.admin.list.all', searchQuery.get());
const loading = !subscription.ready();
const allEvents = Events.find();
//loop through cursor, if event recurs - create new document for the event and insert it in to LocalEvents collection
allEvents.forEach(function(event) {
if (event.eventRecurs) {
//grab array of all recurring dates
const dates = event.recurringDates;
//loop through recurring dates and create new event document for each with a random ID and a parentId of the original event
_.each(dates, (date) => {
LocalEvents.insert({
'_id': Random.id(),
'name': event.name,
'description': event.description,
'defaultDate': date,
'parentId': event._id
});
})
} else {
//if event doesn't recur, set parentId and insert into local collection
event.parentId = event._id;
LocalEvents.insert(event);
}
});
const events = LocalEvents.find({}, { sort: { defaultDate: 1 }}).fetch();
//pass everything to the component
return { loading, events, searchQuery };
}, AdminEventsAll);
我的問題是,當我訂閱的變化(通過使用一個搜索欄) - 它似乎是本地集合仍持有它的一些文檔,因爲有重複實例的事件。每當在我的用戶界面的搜索欄中按下某個鍵時,我傳遞給我的訂閱的參數(searchQuery)就會被設置 - 這會創建一個被動搜索。當我輸入時,對於我輸入的每個字母,我都會看到文檔重複。它基本上表明,當地收藏是用相同的文件重新填充,所以它們一遍又一遍地重複。
每當searchQuery發生變化時,如何確保集合在我的容器中正確清空?
在此先感謝
**編輯 - 發現其解決**
我說我找到一個答案