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!