流星聚合结果未显示在模板中 [英] Meteor Aggregated result not showing in template

查看:40
本文介绍了流星聚合结果未显示在模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注这个 关于 stackoverflow 的答案.在控制台中,我得到了正确的总和.

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');

我的模板助手:

Template.statsBrief.helpers({
    hoursTotalSum: function() {
        var currentUserId = Meteor.userId();
        return Reports.find({userId: currentUserId});
    },
});

我的模板:

{{#each hoursTotalSum}} {{hours}} {{/each}}

Meteor 控制台返回:

Meteor console retunrs this:

[ { _id: 'F8ZEWeKMoRfXaEGNa', hours: 30 },
I20151221-09:57:09.097(0)?   { _id: 'ufALZHfhWyQ8pMkgD', hours: 85 } ]

Meteor 模板返回:

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.

推荐答案

我通过 meteorhacks 成功地使用了聚合:聚合 包,但是我的做法与您的示例略有不同.我将展示如何实现相同的目标,而不是调试您的代码.

I successfully use aggregates via the meteorhacks:aggregate package however I do it a bit differently from your example. Rather than debug your code I will show how I would achieve the same goal.

注意:下面的代码是在coffeescript中

Note :The code below is in coffeescript

编写 Meteor 方法

创建一个服务器端 Meteor 方法来执行聚合.您不需要发布您的聚合,因为所有处理都在服务器上进行,结果通过$project步骤在您的控制之下.

Create a server side Meteor Method to perform the aggregation. You do not need to publish your aggregation as all the processing happens on the server and the results are under your control via the $project step.

Meteor.methods 
    getHoursTotal: ->
        return null unless @userId
        check @userId, String
        pipeline = [
            {$group:{'_id': @userId, 'hours':{$sum:'$hours'}}},
            {$project: 
                _id:false, 
                userId:'$_id', 
                hours: '$hours', 
            }
        ]
        return reports.aggregate pipeline

在您的代码中,您正在处理结果中的每个项目.看起来您只是想将 _id 重命名为 userId 并包含计算出的 hours 字段.我在 $project 步骤中做到了这一点.

In your code you were processing each item in the result. It looked like you were just wanting rename _id to userId and include the calculated hours field. I did this in the $project step instead.

调用 Meteor 方法

您的代码取决于可用的 userId,因此我们只想在用户登录后运行它.我们可以使用 tracker 使其成为反应式.>

Your code depends on the userId being available, so we only want to run it after the user has signed in. We can make this reactive using tracker.

Tracker.autorun ->
    return unless Meteor.userId()?
    Meteor.call 'getHoursTotal', (err, res) ->
        Session.set 'hoursTotal', res

现在代码只有在 Meteor.userId 不为空时才会调整,当它运行时 hoursTotal 将存储在 Session 中> 准备使用.

Now the code will only tun if the Meteor.userId is not null and when it does run the hoursTotal will be stored in the Session ready for use.

公开结果

为了很好地公开结果,最好将其放在模板助手上.

To expose the results nicely it is a good idea to put it on a Template helper.

Template.statsBrief.helpers
    hoursTotal: -> Session.get 'hoursTotal'

我认为以这种方式执行聚合和公开结果更简单,同时还可以从完全响应式中受益.

I think performing the aggregate and exposing the results in this way is simpler whilst also benefiting from being fully reactive.

这篇关于流星聚合结果未显示在模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆