Javascript Meteor-Helper在每次刷新页面时将重复数据插入到集合中
我在meteor中实现通知时遇到问题。我在助手中有这个代码。它可以正常工作,并为当前用户提供有关其订阅内容的正确通知。但是,每当我刷新页面时,相同的通知都会两次插入到通知集合中,在ui中显示相同通知的用户副本。有人知道怎么解决这个问题吗Javascript Meteor-Helper在每次刷新页面时将重复数据插入到集合中,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我在meteor中实现通知时遇到问题。我在助手中有这个代码。它可以正常工作,并为当前用户提供有关其订阅内容的正确通知。但是,每当我刷新页面时,相同的通知都会两次插入到通知集合中,在ui中显示相同通知的用户副本。有人知道怎么解决这个问题吗 Template.notifications.helpers({ notifications : function(){ Meteor.subscribe('theNotificationStatus'); Meteor.subscrib
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});
}
});
助手应该没有副作用。他们应该读取并返回数据,而不是插入/更新/修改数据库。我们有这个规则是因为,正如您所发现的,助手可以在不可预知的时间运行。请参阅fore的“过度工作的助手”部分了解更多信息。是否还有其他方法可以解决此问题?只是暂时的解决办法。