【发布时间】:2015-12-22 04:43:49
【问题描述】:
我正在关注这个answer on stackoverflow。在控制台中,我得到了正确的总和。
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();
});
我已经订阅了在客户端创建的新集合:
Meteor.subscribe('hoursTotalSum');
我的模板助手:
Template.statsBrief.helpers({
hoursTotalSum: function() {
var currentUserId = Meteor.userId();
return Reports.find({userId: currentUserId});
},
});
我的模板:
{{#each hoursTotalSum}} {{hours}} {{/each}}
Meteor 控制台返回这个:
[ { _id: 'F8ZEWeKMoRfXaEGNa', hours: 30 },
I20151221-09:57:09.097(0)? { _id: 'ufALZHfhWyQ8pMkgD', hours: 85 } ]
Meteor 模板返回:
50 20 15
虽然求和正确,并且后端控制台确认了这一点,但我仍在努力将值放入模板中。
【问题讨论】:
标签: meteor