简体   繁体   中英

array not displaying in template, Meteor

i have the following array as an example:

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"
                                                  ]
                                     ]
               ]
            ] 

and i'm trying to display it on a template:

<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>

The array is passed by a template helper, i'm passing it to test the template, and later it will be replaced by a database reading plus a function that will arrange everything to look as it looks in "myarray":

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

The problem is, the template displays nothing, I've been looking for the problem but can't seem to figure it out, the console displays no errors so i'm lost here.

First of all, your array isn't valid syntax. I'm guessing that deviceN , variables and variableN are meant to be objects, not more arrays? like so:

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"
      }
    ]
  }
];

With the above, the rest of your code should render fine.

I'm surprised you didn't get any errors, if I copy paste your data into devtools it breaks immediately

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM