Reactive aggregation - Help!


#1

I have been through about every example of aggregation and I can’t get it to work. I must be missing something small so here you go. Any help will be greatly appreciated.

First, what i want to accomplish is this. I have a table of transactions that I want to sum by Part/Movement and display the summed quantity by movement. But I can’t get anything to display on the screen.

This is what I get from Mongo.

db.movements.aggregate( [ { $group: { _id: “$Movement”, Quantity: { $sum: “$Quantity” } } } ] )
{ “_id” : “Complete”, “Quantity” : 50 }
{ “_id” : “Wip”, “Quantity” : 20 }
{ “_id” : “InStock”, “Quantity” : 20 }
{ “_id” : “OnOrder”, “Quantity” : 10 }

Here is my code.

Server: main.js

Meteor.publish("reportTotals", function() {
// Remember, ReactiveAggregate doesn't return anything
    ReactiveAggregate(this, Movements, [{
        
        $group: {
            '_id': '$Movement',
            'quantity': {
            // In this case, we're running summation. 
                $sum: '$Quantity'
                        }
        }
    }, {
        $project: {
        
            quantity: '$Quantity'

        } // Send the aggregation to the 'clientReport' collection available for client use
    }], { clientCollection: "clientReport" });
});

client side js

Template.pstocks.helpers({

  reportTotals: function() {
        console.log("I'm working");
        return clientReport.find();
    },


});

html file:

{{#each reportTotals}}
  Total Hours: {{quantity}}
  {{/each}}


#2

I have personally never used ReactiveAggregate but from https://github.com/JcBernack/meteor-reactive-aggregate it seems you are doing things right in the publish (counter intuitive not to have a return).

Here is how I would debug this:

  • Where is your subscribe?
  • I am not sure you are using the #each syntax properly - please look here: http://blazejs.org/guide/spacebars.html#Each-in
  • In the browser console, check that the collection ClientReport has something (ClientReport.find().fetch())
  • Then add a breakpoing in your reportsTotals helper to make sure it’s being called and check its output

#3

Thanks. I finally found the issue. I had clientReport in my helper instead of ClientReport.