0

On my server side, I publish a Groups collection. This relies on publishing groups that match another Servers collection.

Meteor.publish('groups', function() {   
    const servers = Servers.find({}); // simplified code
    return Groups.find({serverId: {$in: servers}});
});

A client view then subscribes to that:

self.autorun(() => {
    self.subscribe('groups');
});

This generally works fine. The problem comes when I add insert a server in the Servers collections, there is no indication to the publication to update the Groups, therefore this added server's Groups don't publish to the client.

What is the right way to handle this?

dthree
  • 19,847
  • 14
  • 77
  • 106
  • See https://stackoverflow.com/questions/43167667/inconsistent-updating-of-meteor-template/43183145#43183145 => look for server side _reactive_ publication – ghybs May 04 '17 at 04:40

2 Answers2

0

The Meteor Guide is always a good starting point for these kind of problems. Go here https://guide.meteor.com/data-loading.html#publishing-relations. TLDR; just add reywood:publish-composite package. :)

Let me know.

gone43v3r
  • 397
  • 1
  • 6
0

A possible straightforward approach here is to create a separate publication for Servers and then make groups Servers-dependent.

// Server
Meteor.publish("servers", function() {
  return Servers.find({}, { fields: { _id: 1 } });
});

Meteor.publish("groups", function(serversIds) {
  return Groups.find({ serverId: { $in: serversIds } });
});

// Client
self.autorun(() => {
  self.subscribe("servers");
});

self.autorun(() => {
  const serversIds = Servers.find().map(server => server._id);
  self.subscribe("groups", serversIds);
});
Ramil Muratov
  • 548
  • 5
  • 13