# Java script basics

**URL:** https://discourse.nodered.org/t/java-script-basics/3049
**Category:** Dashboard
**Created:** [11 September 2018 18:37 UTC](https://discourse.nodered.org/t/java-script-basics/3049 "2018-09-11T18:37:41Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![fil](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@fil](https://discourse.nodered.org/u/fil)
#### Post date: [11 September 2018 18:37 UTC](https://discourse.nodered.org/t/java-script-basics/3049/1 "2018-09-11T18:37:41Z")

</div>

Trying to optimise my flows a bit but am having problems i cant quite fathom.

i have 4 x function nodes i think i can replace with 1. Each is passed the same message containing sensor readings and has to reference a flow level variable to determine the record source.  
(the preceding node overwrites the topic).

so in each function i am retrieving the flow level variable that indicates the sensor data source and using that to recreate a meaningful topic hierarchy, including the sensor type that function is devoted to, and resetting the message payload to the payload.sensor value of interest before passing on the new message.

the individual function devoted to temperatures :

```
var plant = flow.get('plant')||'PlantU';
var part1 = plant.slice(0,5);
var part2 = plant.slice(-1);
plant = part1 +'/' + part2;
msg.payload = msg.payload.temperature;
msg.topic = 'Notify/'+ plant +'/T';
return msg;

```

Works without errors and the other functions mirror what its doing too

and this is my combined funtion,

```
var plant = flow.get('plant')||'PlantY';
var part1 = plant.slice(0,5);
var part2 = plant.slice(-1);
plant = part1 +'/' + part2;
var msg1 = { payload:0 };
var msg2 = { payload:0 };
var msg3 = { payload:0 };
var msg4 = { payload:0 };
//moisture msg1
msg1.payload = msg.payload.moisture;
msg1.topic = 'Notify/'+ plant +'/M';
// battery msg2
msg2.payload = msg.payload.battery;
msg2.topic = 'Notify/'+ plant +'/B';
// Temp msg3
msg3.payload = msg.payload.temperature; //.payload.temperature;
msg3.topic = 'Notify/'+ plant +'/T';
//EC msg4
msg4.payload = msg.payload.conductivity;
msg4.topic = 'Notify/'+ plant +'/EC';
return [msg1,msg2,msg3,msg4]

```

it kicks out this error

```
11/09/2018, 19:23:51[node: Notification machine](http://10.0.0.110:1880/#)function : (error)

"TypeError: Cannot read property 'temperature' of undefined"

```

it then continues to work as i would expect, and kick out the correct sensor values with the correct topics

So should i just ignore the error??

---

<div class="post-metadata">

### Author: ![fil](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@fil](https://discourse.nodered.org/u/fil)
#### Post date: [11 September 2018 18:44 UTC](https://discourse.nodered.org/t/java-script-basics/3049/2 "2018-09-11T18:44:04Z")

</div>

ok i just moved the payload assignments up to the declarations and the error has gone away !!

i have to leave the room for a little screaming..

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [11 September 2018 19:14 UTC](https://discourse.nodered.org/t/java-script-basics/3049/3 "2018-09-11T19:14:08Z")

</div>

I could not reproduce the error. I just crafted an input the flow with the required properties:

```auto
{
    "moisture": 30,
    "battery": 40,
    "temperature": 50,
    "conductivity": 60
}

```

Also, if you want to send all four messages out of the function node than you need to change the last line to:

```auto
return [[msg1,msg2,msg3,msg4]]

```

If your leave

```auto
return [msg1,msg2,msg3,msg4]

```

then your function node will need 4 outputs.

---

<div class="post-metadata">

### Author: ![fil](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@fil](https://discourse.nodered.org/u/fil)
#### Post date: [11 September 2018 19:25 UTC](https://discourse.nodered.org/t/java-script-basics/3049/4 "2018-09-11T19:25:38Z")

</div>

Thanks Andrei,

After my initial post i thought i may as well move the final payload assignments to where i am declaring the new messages, after doing so the error stopped appearing in the debug window?  
i had copied the declaration statement directly from a node red guide page.

But now no error??? alls well i guess..

I guess i must have left a stray ',' or invisible character in the text i have pasted a lot as i dont know any java.. ..

re the output, i am currently using 4 discrete output nodes and directing all 4 paths back to the same mqtt out node, with your suggestion i could reduce the flows to 1 and send out all 4 messages in an array? sequentially?, Ok i will give that a go.. thanks again for taking the time to look at this?

---

<div class="post-metadata">

### Author: ![fil](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@fil](https://discourse.nodered.org/u/fil)
#### Post date: [11 September 2018 19:33 UTC](https://discourse.nodered.org/t/java-script-basics/3049/5 "2018-09-11T19:33:18Z")

</div>

yes the array of messages out of a single node works great.

so far i have managed to reduce the number of nodes employed by a factor of 10

from 300+ to less than 40 and 24 of those are devoted to the ui and retrieving user set settings..

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [11 September 2018 19:35 UTC](https://discourse.nodered.org/t/java-script-basics/3049/6 "2018-09-11T19:35:37Z")

</div>

wow, well done. Excellent simplification, For sure the flow is more readable and easier to maintain now.

---

<div class="post-metadata">

### Author: ![machadotiago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/machadotiago/32/23776_2.png) [@machadotiago](https://discourse.nodered.org/u/machadotiago)
#### Post date: [12 September 2018 14:54 UTC](https://discourse.nodered.org/t/java-script-basics/3049/7 "2018-09-12T14:54:09Z")

</div>

Have you tried to use "Change" and "Switch" instead of "Function" for such a manipulation? I'm pretty sure you can do most of those string managements by only using the "Change" node and increase the readability even more
