0

I have a Meteor application with a publish of:

Meteor.publish('my_items', function() {
        var selector = {owner_id: this.userId};
        var items = ItemOwnership.find(selector, {fields: {item_id: 1}}).fetch();
        var itemIds = _.pluck(items, 'item_id');
        return Items.find({
            _id: {$in: itemIds},
            item_archived_ts: { $exists: false }
        });
});

and a subscription of this:

Meteor.subscribe('my_items');

The application allows for the user to add items to the 'Items' collection and this is done by calling a server method. The Items collection on the server is updated with the new record, but the client-side equivalent collection is not showing the new record. Is there anything obviously wrong with what I am doing, or some way to debug this?

p.s. there are no client/server-side errors occurring?

JoeTidee
  • 24,754
  • 25
  • 104
  • 149
  • 1
    This sounds like a [reactive join](https://stackoverflow.com/questions/26398952/meteor-publish-publish-collection-which-depends-on-other-collection) problem. If you modify `ItemOwnership`, it will not reactively update the items published to the client. Using an array instead of a join table is one way to fix this. – David Weldon May 22 '15 at 23:05
  • Thanks for this. In this particular case I could not use an array. Instead, I ended-up using the reywood:publish-composite package to achieve this - I will be posting the answer below. – JoeTidee May 22 '15 at 23:14

1 Answers1

0

I found a way to accomplish this using the reywood:publish-composite Meteor package. Here is the publish that achieves this:

Meteor.publishComposite('my_items', {
    find: function () {
        var selector = {owner_id: this.userId};
        return ItemOwnership.find(selector, {fields: {item_id: 1}});
    },
    children: [
        {
            find: function(IOrecord){
                return Items.find({
                    _id: IOrecord.item_id,
                    item_archived_ts: { $exists: false }
                });
            }
        }
    ]
});
JoeTidee
  • 24,754
  • 25
  • 104
  • 149