I am following this answer on stackoverflow. In console, I get the correct aggregated sum.
Meteor.publish('hoursTotal', function() {
var self = this;
var pipeline = [
{$group: {_id: "$userId", hours: {$sum: "$hours" }}}
];
var result = Reports.aggregate(pipeline);
_.each(result, function(result) {
self.added("hoursTotalSum", result._id, {
userId: result._id,
hours: result.hours
});
});
console.log(result);
self.ready();
});
I've subscribed to the new collection created in client:
Meteor.subscribe('hoursTotalSum');
My template helper:
Template.statsBrief.helpers({
hoursTotalSum: function() {
var currentUserId = Meteor.userId();
return Reports.find({userId: currentUserId});
},
});
My template:
{{#each hoursTotalSum}} {{hours}} {{/each}}
Meteor console retunrs this:
[ { _id: 'F8ZEWeKMoRfXaEGNa', hours: 30 },
I20151221-09:57:09.097(0)? { _id: 'ufALZHfhWyQ8pMkgD', hours: 85 } ]
Meteor templates return this:
50 20 15
Although the summation is happening right, and the backend console confirms that, I'm struggling to get the value into the template.