使用助手从用户中检索子集合
Using helper to retrieve subcollections from Users
我有一个项目,每个用户可以有多个费用。费用存储在用户文档中。
例如,用户的文档如下所示:
{
"username": "Joe Bloggs",
"expenses": [
{
"title": "bucket of paint",
"price": 9.99
},
{
"title": "large mop",
"price": 5.49
}
]
}
我正在尝试构建一个助手来输出每个用户的每个费用,最终在页面上会像这样:
Joe Bloggs | Bucket of paint | 9.99
Joe Bloggs | Large mop | 5.49
Cynthia Smith | Small paintbrush | 3.99
如果我只是想获取用户列表,我会这样做:
Template.Expenses.helpers({
allExpenses(){
var allUsers = Meteor.users.find().fetch();
return allUsers;
}
});
...所以现在我将用户放在一个数组中,我将如何遍历每个用户以输出他们的费用详细信息?
非常感谢,
看看这个解决方案,在评论中解释
users = [{ // assuming you have something like this array
"username": "Joe Bloggs",
"expenses": [{
"title": "bucket of paint",
"price": 9.99
}, {
"title": "large mop",
"price": 5.49
}]
}, {
"username": "Dave Peterson",
"expenses": [{
"title": "Small Brush",
"price": 4.59
}, {
"title": "Stud finder",
"price": 19.99
}]
}];
var expenses = [];
users.forEach(function(currentUser) { // loop over users
currentUser.expenses.forEach(function(currentExpense) { // loop over current users expenses
var newExpense = { // create a new expense with needed properties
username: currentUser.username,
title: currentExpense.title,
price: currentExpense.price
};
expenses.push(newExpense); // add new expense to expenses array
});
});
// print the expenses as needed
expenses.forEach(function(expense){
console.log([expense.username, expense.title, expense.price].join(" | "))
});
您可以使用 {{#each}} 或 {{#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;
}
}
我有一个项目,每个用户可以有多个费用。费用存储在用户文档中。
例如,用户的文档如下所示:
{
"username": "Joe Bloggs",
"expenses": [
{
"title": "bucket of paint",
"price": 9.99
},
{
"title": "large mop",
"price": 5.49
}
]
}
我正在尝试构建一个助手来输出每个用户的每个费用,最终在页面上会像这样:
Joe Bloggs | Bucket of paint | 9.99
Joe Bloggs | Large mop | 5.49
Cynthia Smith | Small paintbrush | 3.99
如果我只是想获取用户列表,我会这样做:
Template.Expenses.helpers({
allExpenses(){
var allUsers = Meteor.users.find().fetch();
return allUsers;
}
});
...所以现在我将用户放在一个数组中,我将如何遍历每个用户以输出他们的费用详细信息?
非常感谢,
看看这个解决方案,在评论中解释
users = [{ // assuming you have something like this array
"username": "Joe Bloggs",
"expenses": [{
"title": "bucket of paint",
"price": 9.99
}, {
"title": "large mop",
"price": 5.49
}]
}, {
"username": "Dave Peterson",
"expenses": [{
"title": "Small Brush",
"price": 4.59
}, {
"title": "Stud finder",
"price": 19.99
}]
}];
var expenses = [];
users.forEach(function(currentUser) { // loop over users
currentUser.expenses.forEach(function(currentExpense) { // loop over current users expenses
var newExpense = { // create a new expense with needed properties
username: currentUser.username,
title: currentExpense.title,
price: currentExpense.price
};
expenses.push(newExpense); // add new expense to expenses array
});
});
// print the expenses as needed
expenses.forEach(function(expense){
console.log([expense.username, expense.title, expense.price].join(" | "))
});
您可以使用 {{#each}} 或 {{#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;
}
}