meteorhacks / meteor-aggregate

Proper MongoDB aggregations support for Meteor
MIT License
189 stars 31 forks source link

Template data context not being populated #15

Open LaughingBubba opened 9 years ago

LaughingBubba commented 9 years ago

Hi, I'm trying to render the results of an aggregation within a template but it's not working.

The aggregation works based on the console logging, which is an array of the results. The template is being rendered, just not the rows of the table. Have I missed something patently obvious?

Should should the results be a mongodb cursor?

Here's the Meteor method:

colSamples = new Mongo.Collection("sampleCol");

Meteor.methods({
    getSummary: function () {
        var pipeline =
            [
                { $sort: { primaryKey: 1, secondaryKey: 1 } },
                {
                    $group: {
                        _id: { pk: "$primaryKey", sk: "$secondaryKey", pair: { $concat: ["$result1", "$result2"] } },
                        pairFrequency: { $sum: 1 }
                    }
                }
            ];

        console.log(colSamples.aggregate(pipeline));
        return colSamples.aggregate(pipeline);
    }
});

Here's the route:

  this.route('sampleAgg', {
    data: function () { return Meteor.call("getSummary") }
  });

and the template:

<template name="sampleAgg">
    <hr>
    <h2>Sample Aggregate data</h2>

    <table id="s-agg" class="table table-hover table-striped">

            <thead>
                <tr>
                    <th>Prm Key</th>
                    <th>Snd Key</th>
                    <th>Pair</th>
                    <th>Count</th>
                </tr>
            </thead>

            {{#each this}} 
                <tr>
                    <td>{{pk}}</td>
                    <td>{{sk}}</td>
                    <td>{{pair}}</td>
                    <td>{{pairFrequency}}</td>
                </tr>
            {{/each}}

        <tfoot>
        <tr>
          <th>Prm Key</th>
          <th>Snd Key</th>
          <th>Pair</th>
          <th>Count</th>
        </tr>
        </tfoot>

        </table> 

</template>
wuzhuzhu commented 9 years ago

Maybe the route is in client side, and the Meteor.call on client side is async and not wait for returned values.