2016-03-16 99 views
0

爲什麼查詢在觀察內更改時不會觸發通知?當我把這段代碼放在幫助器中時,它可以工作,但只適用於該幫助器模板。流星 - 觀察未在自動運行功能內部運行

Tracker.autorun(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 of the status's the user should be notified for based on what eventID and categories he/she is subscribed to. 
    var query = Status.find({ $and: [ { eventID: { $in: arrayEvents } } , { category: { $in: arrayCategory } }, { createdBy: { $ne: currentUserID } } ] }, {sort: {date: -1} }); 

    var handle = query.observe({ 
    added: function (id, fields) { 
     sAlert.success('New Notifications', {timeout: '6000'}); 
    }, 
    }); 

    // After five seconds, stop keeping the count. 
    setTimeout(function() {handle.stop();}, 5000); 

}); 
+0

不應該是'Subscriptions.find({userID:currentUserID},{fields:{「eventID」:1}});'? – Guig

+0

似乎沒有區別。仍然卡住! – user3541209

回答

0

你什麼時候把在Template.someTemplate.onCreatedcomponentWillMount如果你使用你作出反應得到什麼記錄?

Meteor.subscribe('theNotificationStatus'); 
Meteor.subscribe('theNotificationSubscriptions'); 
Tracker.autorun(function() { 

    var currentUserID = Meteor.userId(); 
    var subscriptions = Subscriptions.find(
    {userID: currentUserID}, 
    {fields: {eventID: 1, category: 1}} 
); 
    console.log('currently, there are', subscriptions.count(), 'subscriptions loaded'); 

    var arrayEvents = []; 
    var arrayCategory =[]; 

    subscriptions.forEach(function (subscription) { 
    arrayEvents.push(collection.eventID); 
    arrayCategory.push(collection.category); 
    }); 


    //All of the status's the user should be notified for based on what eventID and categories he/she is subscribed to. 
    var query = Status.find({ 
    eventID: { $in: arrayEvents }, 
    category: { $in: arrayCategory }, 
    createdBy: { $ne: currentUserID }, 
    }); 
    console.log('currently, there are', query.count(), 'status loaded'); 

    var handle = query.observe({ 
    added: function (status) { 
     console.log('new status', status); 
    }, 
    }); 
});