我正试图在meteor中格式化一个订单表,我正努力在文档中输出一个嵌套数组。我有以下代码:
收集数据
{
"_id" : "tDLaCMSde3QyneJqm",
"orderNumber" : 1234,
"createdAt" : ISODate("2016-01-11T11:14:21.986Z"),
"productsInOrder" : [
{
"item" : 10300,
"desc" : "Ergonomic Wooden Fish",
"quantity" : "43",
"price" : "0.92",
"lineprice" : "39.56",
"_id" : "BQeEwtGQEDpPxA6ZM"
},
{
"item" : 98517,
"desc" : "Refined Concrete Soap",
"quantity" : "10",
"price" : "2.11",
"lineprice" : "21.10",
"_id" : "YqBdy8aLJovuncQce"
},
{
"item" : 69824,
"desc" : "Incredible Frozen Gloves",
"quantity" : "7",
"price" : "3.79",
"lineprice" : "26.53",
"_id" : "EefPSwLHCFyJuzXcT"
},
{
"item" : 14897,
"desc" : "Intelligent Frozen Towels",
"quantity" : "3",
"price" : "4.15",
"lineprice" : "12.45",
"_id" : "BSg32fTmpqZBdM2eT"
}
]
}
HTML/空格键
<template name="orders">
<table class="table table-striped">
<thead>
<tr>
<th>
Order Number
</th>
<th>
Ordered on
</th>
<th>
Items in order
</th>
</tr>
</thead>
<tbody>
{{#each orders}}
<tr>
<td>
{{orderNumber}}
</td>
<td>
{{createdAt}}
</td>
<td>
{{productsInOrder}}
</td>
</tr>
{{/each}}
</tbody>
</table>
</template>
JS/助手
Template.orders.helpers({
'orders': function() {
return Orders.find({});
}
});
渲染输出
如您所见,“按顺序排列的项目”没有显示正确的信息。你能帮我解释一下我错在哪里吗?多谢提前。
最佳答案
通过将producstInOrder
放入double-braces空格键,可以将变量序列化为字符串,从而将每个对象序列化为'[object object]'javascript告诉您存储的变量是object类型的。如果要显示列表中每个项目的项目编号,则需要使用另一个each块循环productsInOrder
,并呈现项目字段:
{{#each producstInOrder}}
{{item}}
{{/each}}
您还可以在
productsInOrder
中包含的项上呈现任何其他可用字段。关于javascript - meteor 检索嵌套数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34720889/