无法在流星模板中显示聚合值

时间:2015-11-17 20:42:47

标签: meteor aggregation

我能够从服务器到客户端获取aggreate值,但无法在模板上显示它。我在这里错过了什么。感谢你的帮助。我是流星的新手。

//客户端javascript

Template.DashboardCategoriesChart.helpers({
    'CategoryAggregateItem':function(){
        var res;
        Meteor.call("getAggregateCategoriesSum",function(errors,results){
            console.log("results value: "+ JSON.stringify(results))
            return results ;
        };
    };
});

//返回stringfy值

results value: [
    {"_id":"Household","totalAmount":420},
    {"_id":"Insurance","totalAmount":235},
    {"_id":"Family","totalAmount":1358},
    {"_id":"Utilities","totalAmount":5371.5},
    {"_id":"Automobile","totalAmount":500},
    {"_id":"Home Office","totalAmount":290},
    {"_id":"Travel","totalAmount":14},
    {"_id":"Food","totalAmount":303}
]

//模板

{{#each  CategoryAggregateItem}}
    <tr> 
        <td>{{_id}}</td><td>{{totalAmount}}</td> 
    </tr> 

{{/each}}

2 个答案:

答案 0 :(得分:0)

尝试将您的客户端javascript更改为:

Template.DashboardCategoriesChart.onCreated(function () {
  _this = this;
  _this.CategoryAggregateItem = new ReactiveVar(null);
  Meteor.call("getAggregateCategoriesSum",function(errors,results){
    console.log("results value: "+ JSON.stringify(results))
    _this.CategoryAggregateItem.set(results);
  }
});

Template.DashboardCategoriesChart.helpers({
  'CategoryAggregateItem':function(){
    return Template.instance().CategoryAggregateItem.get();
  });

当触发来自Meteor.Call的回调时,它会更新ReactiveVar。这会导致模板重新渲染并显示检索到的数据 在客户收到数据之前,您可能还希望在模板中提供替代标记,以便帮助程序返回null

答案 1 :(得分:0)

以下代码有效,感谢JeremyK带来了正确的方向。

 Template.DashboardCategoriesChart.helpers({
'CategoryAggregateItem':function(){
    return Template.instance().CategoryAggregateItem.get(); //this.CategoryAggregateItem;
    }
});
Template.DashboardCategoriesChart.onCreated(function () {
    var instance = this;
    this.CategoryAggregateItem = new ReactiveVar(null);
    Meteor.call("getAggregateCategoriesSum",function(errors,results){
    console.log("results value: "+ JSON.stringify(results))
    instance.CategoryAggregateItem.set(results);
    })
})