I have a project where each User can have several Expenses. Expenses are stored in the User document.
For example, a user's document looks like this:
{
"username": "Joe Bloggs",
"expenses": [
{
"title": "bucket of paint",
"price": 9.99
},
{
"title": "large mop",
"price": 5.49
}
]
}
I'm trying to build a helper that outputs each Expense of every User, which would end up like this on the page:
Joe Bloggs | Bucket of paint | 9.99
Joe Bloggs | Large mop | 5.49
Cynthia Smith | Small paintbrush | 3.99
If I were just trying to get a list of users, I'd do something like this:
Template.Expenses.helpers({
allExpenses(){
var allUsers = Meteor.users.find().fetch();
return allUsers;
}
});
... so now I have the users in an array, how would I loop through each user to output their Expense details?
Many thanks,
You can directly loop through your users with {{#each}} or {{#each in}}
<template name="Expenses">
{{#each user in allUsers}}
{{#each expense in user.expense}}
<p>{{user.username}} | {{expense.title}} | {{expense.price}}</p>
{{/each}}
{{/each}}
</template>
Template.Expenses.helper(){
allUsers(){
var allUsers = Meteor.users.find().fetch();
return allUsers;
}
}