簡體   English   中英

數組未顯示在模板(流星)中

[英]array not displaying in template, Meteor

我有以下數組作為示例:

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;
     }
});

問題是,模板什么也不顯示,我一直在尋找問題,但似乎無法解決,控制台沒有顯示錯誤,所以我在這里迷路了。

首先,您的數組不是有效的語法。 我猜想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"
      }
    ]
  }
];

有了上面的內容,其余的代碼應該可以正常顯示。

我很驚訝您沒有遇到任何錯誤,如果我將您的數據復制粘貼到devtools中,它將立即中斷

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM