javascriptmongodbmeteoriron-routermeteor-helper

How to access aggregated collection data in meteor client?


I aggregated some data and published it, but I'm not sure how/where to access the subscribed data. Would I be able to access WeeklyOrders client collection (which is defined as client-only collection i.e WeeklyOrders = new Mongo.Collection(null);)?

Also, I see "self = this;" being used in several examples online and I just used it here, but not sure why. Appreciate anyone explaining that as well.

Here is publish method:

Meteor.publish('customerOrdersByWeek', function(customerId) {
  check(customerId, String);
  var self = this;
  var pipeline = [
    { $match: {customer_id: customerId} },
    { $group: {
        _id : { week: { $week: "$_created_at" }, year: { $year: "$_created_at" } },
        weekly_order_value: { $sum: "$order_value" }
      }
    },
    { $project: { week: "$_id.week", year: "$_id:year" } },
    { $limit: 2 }
  ];
  var result = Orders.aggregate(pipeline);
  result.forEach(function(wo) {
    self.added('WeeklyOrders', objectToHash(wo._id), {year: wo.year, week: wo.week, order_value: wo.weekly_order_value});
  });
  self.ready();
});

Here is the route:

Router.route('/customers/:_id', {
  name: 'customerOrdersByWeek',
  waitOn: function() {
    return [
      Meteor.subscribe('customerOrdersByWeek', this.params._id)
    ];
  },
  data: function() { return Customers.findOne(this.params._id); }
});

Here is my template helper:

Template.customerOrdersByWeek.helpers({
  ordersByWeek: function() {
    return WeeklyOrders.find({});
  }
});

Solution

  • You want var self = this (note the var!) so that call to self.added works. See this question for more details. Alternatively you can use the new es6 arrow functions (again see the linked question).

    There may be more than one issue where, but in your call to added you are giving a random id. This presents two problems:

    1. If you subscribe N times, you will get N of the same document sent to the client (each with a different id). See this question for more details.
    2. You can't match the document by id on the client.

    On the client, you are doing a Customers.findOne(this.params._id) where this.params._id is, I assume, a customer id... but your WeeklyOrders have random ids. Give this a try:

    self.added('WeeklyOrders', customerId, {...});
    

    updated answer

    You'll need to add a client-only collection as a sort-of mailbox for your publisher to send WeeklyOrders to:

    client/collections/weekly-orders.js

    WeeklyOrders = new Meteor.Collection('WeeklyOrders');
    

    Also, because you could have multiple docs for the same user, you'll probably need to:

    1. Forget what I said earlier and just use a random id, but never subscribe more that once. This is an easy solution but somewhat brittle.

    2. Use a compound index (combine the customer id + week, or whatever is necessary to make them unique).

    Using (2) and adding a customerId field so you can find the docs on the client, results in something like this:

    result.forEach(function (wo) {
      var id = customerId + wo.year + wo.week;
      self.added('WeeklyOrders', id, {
        customerId: customerId,
        year: wo.year,
        week: wo.week,
        order_value: wo.weekly_order_value,
      });
    });
    

    Now on the client you can find all of the WeeklyOrders by customerId via WeeklyOrders.find({customerId: someCustomerId}).

    Also note, that instead of using pub/sub you could also just do all of this in a method call. Both are no-reactive. The pub/sub gets you collection semantics (the ability to call find etc.), but it adds the additional complexity of having to deal with ids.