流星检索嵌套数组

时间:2016-01-11 11:51:42

标签: javascript mongodb meteor meteor-blaze spacebars

我正在尝试在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 / 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 /助手

Template.orders.helpers({
  'orders': function() {
    return Orders.find({});
  }
});

渲染输出

enter image description here 正如您可以看到&#39;项目顺序&#39;没有显示正确的信息。请问你能帮我解释一下我的错误吗?非常感谢提前。

1 个答案:

答案 0 :(得分:2)

producstInOrder放在double-braces空格键中将变量序列化为字符串,这会导致每个对象被序列化为&#39; [object Object]&#39; JavaScript告诉你存储的变量的方式是object类型。如果您想要显示列表中每个项目的项目编号,您需要使用另一个each块循环productsInOrder并呈现项目字段:

{{#each producstInOrder}}
    {{item}}
{{/each}}

您还可以在productsInOrder

中包含的项目上呈现任何其他字段