Search code examples
javascriptmeteorpublish-subscribemongo-collection

Meteor.publish on server doesn't show new documents on client


The problem is the next code on server:

Meteor.publish(null , function() {
    let events = [];
    Groups.find({participants: this.userId}).forEach(function(item) {
        events.push(item.latestEvent);
    });
    return Events.find({_id: {$in: events}});
});

doesn't provide possibility to see new documents on the client > Events.find().fetch() without reloading the page.

Both collection are in the lib folder:

Groups = new Mongo.Collection('groups');
Events = new Mongo.Collection('events');

I'm pretty sure the issue is in reactive source of data, but still cannot fix it.

Thank You for help!


Solution

  • Yes, you are right: only Events collection is reactive. There is simple way to solve it by using publish-composite package:

    Meteor.publishComposite(null, {
        find(){
          return Groups.find({participants: this.userId});
        },
        children: [{
          find(group){
             return Events.find({_id: {$in: group.latestEvent}});
          }
        }]
    });
    

    But this solution has one disadvantage: Groups documents are published as well. So, probably you should exclude some fields from it.