Need help troubleshooting a subscription


#1

I have a shopping list app with 3 collections: categories, items, deals. My shopping list loops through the categories and imports a template:

{{#each categories}}
       {{ > shoppingListCategory }}
{{/each}}

The shoppingListCategory in turn loops through items like this, passing in the current category to the helper function:

    {{#each listItems category}}
            {{ > itemDeals}}
    {{/each}}

The helper function looks like this:

Template.shoppingListCategory.helpers({
    listItems: function (cat) {
        return ShoppingList.find({category: cat})
    },

    numDeals: function (cat, item) {
        var cnt = Deals.find({category: cat, item: item}).count();
        return cnt ? cnt : null;
    }
})

This part works as expected. The next iteration is where things break down. The itemDeals template works exactly the same way, passing in the current category & item to the helper function:

{{#if hasDeals category item}}
<ul class="collection">
    {{#each itemdeals category item}}
        <li class="collection-item">{{store}}: {{deal}}</li>
    {{/each}}
</ul>
{{/if}}

Template.itemDeals.helpers({

    itemdeals: function (cat, item) {
        //console.log('itemdeals: ', cat, item, this);
        console.log(Deals.find({category: cat, item: item}).fetch());
        return Deals.find({category: cat, item: item})
    },

    hasDeals: function (cat, item) {
        var cnt = Deals.find({category: cat, item: item}).count();
        return cnt ? true : false;
    }
});

I must be missing something really basic. In this case, no deals are found for any of the items. But they are there! I’ve logged out this and the different iterations, all looks in order.

From the browser console, I manually search for deals:

Deals.find({category: "Cereal", item: "Oatmeal"}).fetch()

Which yields:

_id: "uXBzfTS3x2L7vswuF"
category: "Cereal"
deal: "Quaker Oats Buy 1, Get 1 Free"
expiresAt: Sun Aug 16 2015 21:43:19 GMT-0400 (EDT)
foundAt: Sun Aug 16 2015 21:43:19 GMT-0400 (EDT)
item: "Oatmeal"
store: "Publix"
type: "store"

There is one very strange clue. Within itemdeals, when I log out console.log('itemdeals: ', cat, item, this);, I get 9 log entries, which exactly matches my test data. However, when I log out console.log(Deals.find({category: cat, item: item}).fetch()); as shown above, I get 13 log entries. The first 9 are empty, but then the first four items are repeated and actually show the correct Deals.find() results. Very strange.

Maybe I found a Meteor bug?? This is a mind bender. Thanks for any help you can offer!


#2

Just to give an update, I reinstalled the autopublish package and commented out my publications/subscriptions. The above code works fine. Something really strange going on here. My pub/subs are just plain old Meteor.publish() & Meteor.subscribe(). No security yet or anything fancy. Hmmm.


#3

I think you need to leave out the .fetch() in your helper function. You don’t have it in the first code block, which you said works fine.


#4

Thanks for the reply. The .fetch() is only in the console.log().

You know how you stare at something so long and cannot see a problem. In this case, I deleted the files in question and started from scratch just in case I fat-fingered something. Same result. With autopublish, everything works perfect. Without autopublish, not getting the correct results, but only for the very inner nested collection. I have to be missing something basic…or I hit a strange bug.

Collections are in /lib/collections.js
My publications are in /server/publications.js and look like this:

Meteor.publish('deals', function () {
    return Deals.find();
});

My subscriptions are in /client and look like this:

Meteor.subscribe('deals');

#5

I have no idea what I did, but after registering an onrendered/autorun/afterflush function to initialize some javascript this all works…even after removing autopublish. Consider this closed.