【问题标题】:array not displaying in template, Meteor数组未显示在模板中,流星
【发布时间】:2018-06-01 02:53:19
【问题描述】:

我以下面的数组为例:

var myarray = [
               device1: [ name:device1 , 
                          variables: [ variable1: [  name: variable1,
                                                     unit: "a unit",
                                                     value: "a value"
                                                   ],
                                       variable2: [  name: variable2,
                                                     unit: "a unit",
                                                     value: "a value"
                                                  ]
                                     ]
               ], 
               device2: [ name:device2 , 
                          variables: [ variable1: [
                                                  name: variable1,
                                                  unit: "a unit",
                                                  value: "a value"
                                                  ]
                                     ]
               ]
            ] 

我正在尝试在模板上显示它:

<body>
  <div class="container">
    <header>
      <h1>MQTT Device Status List</h1>
    </header>

    <ul>
      {{#each mqttmessages2}}
        {{> mqttmessage2}}
      {{/each}} 
    </ul>

  </div>
</body>


<template name="mqttmessage2">
  <li>{{name}} :
    <ul>
       {{#each variables}}
         <li>{{name}} : {{value}} [ {{unit}} ]  </li>
       {{/each}}
    </ul>
   </li>
</template>

数组由模板助手传递,我传递它来测试模板,稍后它将被数据库读取加上一个函数替换,该函数将安排所有内容以使其在“myarray”中看起来:

Template.body.helpers({
     mqttmessages2() {
                   console.log(myarray);
                   return myarray;
     }
});

问题是,模板什么也没显示,我一直在寻找问题,但似乎无法解决,控制台没有显示任何错误,所以我在这里迷路了。

【问题讨论】:

    标签: javascript arrays node.js templates meteor


    【解决方案1】:

    首先,您的数组不是有效的语法。我猜deviceNvariablesvariableN 应该是对象,而不是更多的数组? 像这样:

    var myarray = [
      {
        name: device1,
        variables: [
          {
            name: variable1,
            unit: "a unit",
            value: "a value"
          },
          {
            name: variable2,
            unit: "a unit",
            value: "a value"
          }
        ]
      },
      {
        name: device2,
        variables: [
          {
            name: variable1,
            unit: "a unit",
            value: "a value"
          }
        ]
      }
    ];
    

    有了以上内容,您的其余代码应该可以正常渲染了。

    我很惊讶您没有收到任何错误,如果我将您的数据复制粘贴到开发工具中,它会立即中断

    【讨论】:

      猜你喜欢
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 2014-04-01
      • 2016-01-27
      • 2015-06-11
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多