流星检索嵌套数组
Meteor retrieving nested arrays
我正在尝试在 Meteor 中格式化 table 订单,但我正在努力输出文档中的嵌套数组。我有以下代码:
采集数据
{
"_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/Spacebars
<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/Helpers
Template.orders.helpers({
'orders': function() {
return Orders.find({});
}
});
渲染输出
如您所见,'Items in order' 没有显示正确的信息。请你能帮忙解释一下我哪里出错了吗?非常感谢。
通过将 producstInOrder
放入 double-braces spacebars serializes the variable to a string, which results in each object being serialized to '[object Object]' JavaScript's way of telling you the variable stored is of type object. If you would want to display the item number for each item in the list you need to loop over productsInOrder
with another each 块并呈现项目字段:
{{#each producstInOrder}}
{{item}}
{{/each}}
您还可以呈现 productsInOrder
中包含的项目的任何其他可用字段。
我正在尝试在 Meteor 中格式化 table 订单,但我正在努力输出文档中的嵌套数组。我有以下代码:
采集数据
{
"_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/Spacebars
<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/Helpers
Template.orders.helpers({
'orders': function() {
return Orders.find({});
}
});
渲染输出
通过将 producstInOrder
放入 double-braces spacebars serializes the variable to a string, which results in each object being serialized to '[object Object]' JavaScript's way of telling you the variable stored is of type object. If you would want to display the item number for each item in the list you need to loop over productsInOrder
with another each 块并呈现项目字段:
{{#each producstInOrder}}
{{item}}
{{/each}}
您还可以呈现 productsInOrder
中包含的项目的任何其他可用字段。