Meteor - Helper每次刷新页面时都会将数据重复插入到集合中

时间:2016-03-15 18:35:02

标签: javascript mongodb meteor

我在尝试在meteor中实现通知时遇到问题。我在帮助器中有这个代码。它工作正常,并为当前用户提供他们订阅的内容的正确通知。但是,无论何时刷新页面,都会在通知集合中插入两次相同的通知,显示用户在ui中复制相同的通知。任何人都知道如何解决这个问题?

Template.notifications.helpers({

  notifications : function(){

    Meteor.subscribe('theNotificationStatus');
    Meteor.subscribe('theNotificationSubscriptions');

    var currentUserID = Meteor.userId();
    var usersEventIds =  Subscriptions.find({userID: currentUserID}, {"eventID": 1});
    var userCategorys = Subscriptions.find({userID: currentUserID}, {"category": 1});
    var arrayEvents = [];
    var arrayCategory =[];

    usersEventIds.forEach(function (collection) {
      arrayEvents.push(collection.eventID);
  });

  userCategorys.forEach(function (collection) {
    arrayCategory.push(collection.category);
});

//All the status's the user should be notified for based on what eventID and categorys he/she is subscribed to.
  var userNotifications = Status.find( { $and: [ { eventID: { $in: arrayEvents  } } , { category: { $in: arrayCategory } } ] } );

//Putting all these status's into a notifications collection.

  userNotifications.forEach(function (collection) {

    var eventID = collection.eventID;
    var category = collection.category;
    var eventName = collection.currentEventName;

    Meteor.call('insertNotificationsData', eventID,category,eventName,currentUserID);
});

Meteor.subscribe('theNotifications');
return Notifications.find({currentUserID:currentUserID});

}

});

0 个答案:

没有答案