How to reactively aggregate mongodb in meteor

前端 未结 2 1374
一向
一向 2021-01-28 06:35

I am newbie to meteor. I had established the publish / subscribe concept. I am facing the following error while performing aggregation reactively.

client code :

相关标签:
2条回答
  • 2021-01-28 07:09

    You don't have a client side collection. Also, you need to subscribe before you call this helper.

    Try this

    import { Template } from 'meteor/templating';
    import { ReactiveVar } from 'meteor/reactive-var';
    import './main.html';
    
    var clientReport = new Mongo.Collection('clientReport');
    
    Meteor.subscribe("reportTotals");
    
    Template.header.helpers({
        'tasks': function () {
            console.log("tasks helper called : ");     
            console.log(clientReport.find().fetch());
        },   
    });
    

    You also don't need the pipeline and no autorun on server code, try this:

    AtmData = new Mongo.Collection('atmdata');
    
    Meteor.startup(() => {
      // code to run on server at startup
    /*     AtmData.insert({
            bottles_used: 123,
        }); */
    
    });
    
    
    
    Meteor.publish("reportTotals", function() {
    // Remember, ReactiveAggregate doesn't return anything
    
        ReactiveAggregate(this, AtmData, [{
            // assuming our Reports collection have the fields: hours, books    
            $group: {
                '_id': null,
                'bottles_used': {
                // In this case, we're running summation. 
                    $sum: '$bottles_used'
                    // $sum: 1
                }
            }
            }], { clientCollection: "clientReport" });    
    });
    
    0 讨论(0)
  • 2021-01-28 07:12

    I made an NPM package here: meteor-publish-join. Its main purpose is to publish expensive-aggregated values after a certain amount of time. Hope it will help in your case.

    0 讨论(0)
提交回复
热议问题