Arrays & Objects in Node-RED

#1

how can i access to my data in function node.
i have tested too many things but no one works. exemple
var matriculeString =msg.payload[0][data3];
var matriculeString =msg.data3;, etc ....
im

0 Likes

#2

This page in the docs show you how to use the tool-tips in the debug panel to get the path.
https://nodered.org/docs/user-guide/messages

But it would be also worth looking at one of the javascript array and object tutorials online, I still refer to the w3schools pages
https://www.w3schools.com/js/js_arrays.asp

1 Like

#3

got it. i was missed "" for data3.
var obj = msg.payload[0];
var matriculeString =obj["data3"];
thanks for the pages and for your quick reply.

0 Likes

#4

i have a counter function node

var counter = context.get('counter')|| 0;
msg.count = counter;
      var counter = counter +1;
    context.set('counter',counter);
return msg;

is it possible to make a counter inside mustache template node. i have tried this code but does't work. what do i miss her and thank you.
PS: {{@index}} doesn't show any thing.

<html>
    <body>
        <div style= "float: none;display: block;text-align: right;">
     <p><b>Référence : {{payload.1}}</b></p>
      <b>Matricule :</b>   {{payload.{{count}}.data3}}
         </div>
{{# payload}}
  {{data1}} {{data2}} this is index {{@index}}<br>
{{/ payload}}
    </body>
</html>
0 Likes

#5

Assuming your function is before your template in your flow.
You could add the counter to the message that is passed along the flow

var counter = context.get('counter')|| 0;
     msg.counter = counter +1; 
   context.set('counter',msg.counter);
return msg;
0 Likes

#6

yes i did that. my counter function node is working very well. and it is before the template .
my problem is her :

{{payload.{{count}}.data3}}

it doesn't show any thing

0 Likes

#7

Add a debug node and look for the difference as to where the counter value is compared to your data

0 Likes

#8

Référence : }}

Matricule : .data3}}
this is what i get in debug node. clearly it doesn't work like that. is there any way else to add counter.
0 Likes

#9

You cannot use mustache tags inside other mustache tags like that.

Mustache is know as 'logic-less' templating - which means it is not meant to include logic within the template. If you need to select a particular element to display out of the msg.payload array, you should copy that value to a know property of the message before passing it to the Template.

For example:

var counter = context.get('counter')|| 0;
msg.count = counter;
counter = counter +1;
context.set('counter',counter);
msg.selectedItem = msg.payload[msg.count].data3
return msg;

And then use {{msg.selectedItem}} in your template.

2 Likes

#10

you mean {{selectedItem}}. thank you very much it works perfectly.

0 Likes