0
votes

Meteor Newbie here!

I have a page where all the open orders are displayed. The order details are stored in a collection. A Template helper will return the order details.

Template.delivery.helpers({
  openOrders: function(){
   return Orders.find({status: "open"});
  }
});

The template look some what like this.

{{#if openOrders}}
  {{#each openOrders}}
     Date: {{createdAt}}
     Total items: {{items.length}}
     Location: {{location}} //It prints the location _id
  {{/each}}
{{/if}}

The Order Collection only have the _id of the location. The Location details are stored in a Collection named Locations.

I want to display the location name (which is stored in the Location collection) instead of the _id.

I created a Template helper which returns the Location details, but how can I link these to helpers so that the Location name is displayed instead of Location _id?

2

2 Answers

0
votes

As you're using mongodb in a relational database fashion, you need to install publish-composite package to make sure all the necessary data are subscribed.

0
votes

When you use each, it will set the this to the current thing that is being iterated over. This will allow you to use this in your helper to perform lookups. So in this case, if you're using a helper to get the orders:

orders: function () {
  return Orders.find({ status: "orders" });
}

Then when you iterate over it with {{#each}}, this is set to the current order, meaning your location helper with look like this:

location: function () {
  return Locations.findOne(this.locationId);
}

Putting it all together in the template it would be like:

{{#if Template.subscriptionsReady}}
  {{#each orders}}
    <h1 class="title">Order #{{_id}}</h1>
    {{#with location}}
      <div class="location">
        <span>Latitude: {{ latitude }}</span>
      </div>
    {{/with}}
  {{/each}}
{{/if}}

Keep in mind: this will only work if you also publish your locations.