0

Example:

<template name="list_customers">
  <p><h3>List Customers</h3></p>
  {{#each customers}}
    {{> list_customers_content}}
  {{/each}}
</template>

<template name="list_customers_content">
..display all customer data from "Customers.find({})...

{{> list_customers_content_ip}}

</template>

<template name="list_customers_content_ip">
.. display data from Customers_ip.find({}) based on #each customer object id.
</template>

Template.list_customers.helpers({
  customers() {
    return Customers.find({});
  },
});

How can this be done in fewest possible queries to two different collections, and is there any way to use the customers context object id? Please write full example.

What should the helper for Customers_ip.find() look like?

1 Answer 1

1

You need to pass the current document (which is this inside the #each context) to the list_customers_content template in order to further process it.

The name of the parameter will be the name to access the value in the child template.

Example:

{
  name: "John Doe",
  _id: "1232131",
}


<template name="list_customers">
  <p><h3>List Customers</h3></p>
  {{#each customers}}
    {{> list_customers_content customer=this}}
  {{/each}}
</template>

<template name="list_customers_content">
  <span>Name: {{customer.name}}</span><!-- John Doe -->
  {{> list_customers_content_ip customerId=customer._id}}
</template>

<template name="list_customers_content_ip">
  {{#each customerIp customerId}}
    <span>IP: {{this.ip}}</span>
  {{/each}}
</template>

A helper for customerIp could look like this:

Template.list_customers_content_ip.helpers({

  customerIp(customerId) {
    // assumes, that these documents have
    // a field named "customerId"
    return Customers_ip.find({ customerId }); 
  },

});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply. That makes sense until the last part. So how do i get the customerId to use in my helper to query the DB?
This requires to add a schema of how the documents in this collection look like. Otherwise it will be just guessing.
It's probably something i don't understand. So the customerId is now in the "list_customers_content_ip" template. But to write a helper for this to find data from the db, i need the customerId in my helper. But how do i access the template customerId in my helper to use it in the query? if that makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.