我正在尝试访问主条目中的嵌套成分对象。 camera2
代码的输出为{{recipe.ingredients}}
。我需要输入什么才能让[object Object],[object Object]
输出名称和金额?
数据库中的对象
{{recipe.ingredients}}
代码
{
"_id": "zSetYKmvv2HB6v8hJ",
"name": "Grilled Cheese",
"desc": "Sandwich",
"ingredients": [{
"name": "Bread",
"amount": "2 Slices"
}, {
"name": "Cheese",
"amount": "Lots"
}],
"author": "ttpbGPgzDnqwN8Gg7",
"createdAt": "2015-12-27T22:53:17.729Z",
"inMenu": false
}
答案 0 :(得分:0)
recipe.ingredients[0].name // Will output Bread
recipe.ingredients[1].name // Will output 2 Slices
ingredients
是一个数组,可能会迭代它会给你所有的ingrident's name and amount.
var recipe = {
"_id": "zSetYKmvv2HB6v8hJ",
"name": "Grilled Cheese",
"desc": "Sandwich",
"ingredients": [{
"name": "Bread",
"amount": "2 Slices"
}, {
"name": "Cheese",
"amount": "Lots"
}],
"author": "ttpbGPgzDnqwN8Gg7",
"createdAt": "2015-12-27T22:53:17.729Z",
"inMenu": false
};
recipe.ingredients.forEach(function(el, i){
alert("Ingredient "+i+": "+el.name+", "+el.amount);
});

答案 1 :(得分:0)
由于ingredients
是一个对象数组,因此您需要使用meteors集合语法来呈现项目集。正如@FélixSaparelli在评论中提到的,有一个很好的例子here。例如:
<div class="RecipeList">
<ul>
{{#each recipe.ingredients}}
<li>
<h1>{{name}}</h1>
<p>{{amount}}</p>
</li>
{{/each}}
</ul>
</div>