Not able to display the aggregation values in template

I am able to get the aggreate values from server to
client, but could not display it on the template. Am i missing something
here.Appreciate your help. iam using meteorhacks/meteor-aggregate package.

//client side javascript

Template.DashboardCategoriesChart.helpers({

    'CategoryAggregateItem':function(){

        var res;

        Meteor.call("getAggregateCategoriesSum",function(errors,results){

        console.log("results value: "+ JSON.stringify(results))

        return results ;

}
});

//stringfy value returned

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}]

//Template

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

        {{/each}}

you cannot return async callback there.

best practice is to assign results to some reactive source, for example ReactiveVar and than just return that ReactiveVar in helper.

onCreated template part is your friend

thanks for direction, i have done that but getting the following error. Not sure what iam missing here.
my client.js file
Template.DashboardCategoriesChart.helpers({
‘CategoryAggregateItem’:function(){
return this.CategoryAggregateItem;
}
});
Template.DashboardCategoriesChart.onCreated(function () {
this.CategoryAggregateItem = new ReactiveVar(null);
Meteor.call(“getAggregateCategoriesSum”,function(errors,results){
console.log("results value: "+ JSON.stringify(results))
this.CategoryAggregateItem.set(results);
})
})

Getting the following error.
Exception in delivering result of invoking ‘getAggregateCategoriesSum’: TypeError: Cannot read property ‘set’ of undefined

this in callback have other scope
I suggest in onCreated

var instance = this;

and than in callback use

instance.CategoryAggregateItem.set(results);

in helper

Template.instance().CategoryAggregateItem.get();
1 Like

Look at the meteor-reactive-method package…

Brilliant! works like a charm. got struck for 2 days. Thanks a ton.