0

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.

0

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.