[SOLVED] Template subscription not limiting results


#1

This is very very odd. The code in question:

Template.featuredProds.onCreated(function () {
  this.autorun(() => {
    console.log('limit products to:', Session.get('limitProductsTo'));
    this.subscribe('products', Session.get('limitProductsTo'), function () {
      console.log('setting prod count to', Products.find().count());
      Session.set('products/count', Products.find().count());
    });
    Session.set('products/loaded', false);
  });
});

What’s happening when the page loads:

I see a ‘limit products to: undefined’ at first, but then immediately after:

limit products to: Object {slug: Object} slug: Object $in: Array[5]
setting prod count to 8

So even though it just subscribed to only five products ({$in: ['prod1', 'prod2', 'prod3', 'prod4', 'prod5']}), the product count is somehow 8?! And this is the only place in my entire app where a subscription to products is being set up.

I’m stumped. What am I missing?


#2

Fixed. So, the other bit of corresponding code (below) was within the layout’s onRendered callback which was probably bad I suppose. I moved it to layout’s onCreated and now it works.

  this.autorun(() => {
    if (this.subscriptionsReady()) {
      let trackingId = Cookie.get('trackingId');

      if (!!trackingId) {
        let trackingInfo = TrackingIds.findOne(trackingId);

        if (trackingInfo) {
          Session.set('limitProductsTo', {
            slug: {
              $in: trackingInfo.slugs
            }
          });
          Session.set('excludeContentFields', trackingInfo.excludeFields);
        }
      }
    }
  });