0

it seems like i can't display an array for my namespace

 {{#each reports}}
    {{_id}}
 {{/each}}

my code looks like this

server method.

reports : function(){
      var pipeline = [
        {$group:{_id: "$activity.destination.name","count":{$sum:1 } } },
        {$match:{_id:{$ne:null} }}];    

    var result = Reports.aggregate(pipeline);
    console.log(result);
  return result;

from helper

reports: function(){      
        Meteor.call("reports",function(err, data) {
          arr =[];
          data.forEach(function(doc){
            arr.push(doc);
          });
          console.log(arr);
         return arr;
         });              
    }

in the browser console, the response looks like this

[Object]
 0:Object
 _id: "Balance Sheet and P&L Rates - Current Year"
 count: 2 ..etc

im not sure if its the namespace or its an object array of non-cursors. but it doesn't give me an error. im not sure if what im doing is correct.

Julius Limson
  • 526
  • 9
  • 29
  • You have a scope issue. In your helper, your `return` is not in the helper's scope but in a callback's. This has probably been asked before, let me try to find the question. - Found. Check https://stackoverflow.com/questions/22147813/how-to-use-meteor-methods-inside-of-a-template-helper, your question is probably a duplicate of that. – Kyll Jun 29 '15 at 09:53
  • @Kyll thanks for the response. and please do. it is greatly appreciated. – Julius Limson Jun 29 '15 at 09:54
  • Check https://stackoverflow.com/questions/22147813/how-to-use-meteor-methods-inside-of-a-template-helper. If it solves your issue, mark your question as duplicate or delete it. Glad I could help! – Kyll Jun 29 '15 at 10:00

1 Answers1

1

Meteor.call returns immediately, and the callback is called once the data is back from the server.

Your template is referencing a helper which in turn calls the server method then immediately returns nothing at all. The helper's callback, when it returns, cannot return the data to the template.

In your case you should:

  1. Create a collection for your reports.

  2. Subscribe to the reports collection when visiting the page in question (this is typically done from a route controller if you're using Iron Router.

  3. Create a publish function to publish your reports to the collection.

The last point isn't as straightforwards as usual since you're aggregating the results, but it's still possible with something like this (untested):

Meteor.publish('reports', function(options) {

    var sub = this;

    var pipeline = [
        {$group:{_id: "$activity.destination.name","count":{$sum:1 } } },
        {$match:{_id:{$ne:null} }}];    

    var result = Reports.aggregate(pipeline);
    var arrayLength = reports.length;
    for (var i = 0; i < arrayLength; i++) {
        var report = reports[i];
        sub.added('reports', report._id, report);
    }
})

Then you can just call find on the Reports collection from your helper:

reports: function() {      
    return Reports.find()       
}

Or else return the that same cursor as part of your data context in your Iron Router controller and use the result directly in your template.

tarmes
  • 15,366
  • 10
  • 53
  • 87