简体   繁体   English

如何访问meteor客户端中的聚合收集数据?

[英]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 ie WeeklyOrders = new Mongo.Collection(null);)? 我是否能够访问WeeklyOrders客户端集合(定义为仅客户端集合,即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({});
  }
});

You want var self = this (note the var!) so that call to self.added works. 你想要var self = this (注意var!),以便调用self.added See this question for more details. 有关详细信息,请参阅此问题 Alternatively you can use the new es6 arrow functions (again see the linked question). 或者,您可以使用新的es6箭头功能(再次查看链接的问题)。

There may be more than one issue where, but in your call to added you are giving a random id. 可能存在多个问题,但在您added的电话中,您提供的是随机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). 如果您订阅N次,您将获得发送给客户端的相同文档的N(每个文档具有不同的ID)。 See this question for more details. 有关详细信息,请参阅此问题
  2. You can't match the document by id on the client. 您无法在客户端上按ID匹配文档。

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. 在客户端,你正在做一个Customers.findOne(this.params._id) ,其中this.params._id是我认为的客户ID ...但是你的WeeklyOrders有随机ID。 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: 您需要将仅限客户端的集合添加为发布者的类别邮箱,以便将WeeklyOrders发送到:

client/collections/weekly-orders.js 客户端/收藏/每周,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. 忘掉我之前说过的内容,只使用随机ID,但永远不要订阅一次。 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). 使用复合索引(结合客户ID +周,或任何必要的使其唯一)。

Using (2) and adding a customerId field so you can find the docs on the client, results in something like this: 使用(2)并添加customerId字段以便在客户端上找到文档,结果如下:

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}) . 现在,在客户端上,你可以找到所有的WeeklyOrders通过customerId通过WeeklyOrders.find({customerId: someCustomerId})

Also note, that instead of using pub/sub you could also just do all of this in a method call. 另请注意,您可以在方法调用中完成所有这些操作,而不是使用pub / sub。 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. pub / sub获取集合语义(调用find等的能力),但它增加了必须处理id的额外复杂性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM