# Help with Javascript on Function Node

**URL:** <https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121>\
**Category:** General\
**Created:** [24 March 2021 14:11 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121 "2021-03-24T14:11:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dgillett](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@Dgillett](https://discourse.nodered.org/u/Dgillett)\
**Post date:** [24 March 2021 14:11 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/1 "2021-03-24T14:11:37Z")

</div>

I am Currently using an MQTT Gateway to receive values from a PLC using modbus. Then sending those values to a raspberry pi, and storing them into a SQLite Database. The Values only send to the Raspberry Pi "On Value Change".

I currently have 15 MQTT devices being joined to KEY/Value Pairs, then running through a function to place the values in a table.

 ![Capture1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8f3a283eca586b7e24570cfebae71e1e8cc13976.png)

The issue I am having is that when some values change and others do not the function node is throwing a Undefined error.  
 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/1/11146cf28482b16d0d8124b43d442d805071b735.png)

I am assuming this error is occurring because some values are sending and some are not thus the value is undefined.

My Question is How can I change my Javascript to allow zero to take its place if no value sends through or any suggestions on helping with my problem.

```auto
var newMsg = { payload: msg.payload };
newMsg.topic = `INSERT INTO SSreadings (ProgramNumber, NumOfLeaks, FsFlow, MsFlow, Ds1Flow, SpruFlow, Jc1Flow, Jc2Flow, Jc3Flow, Jc4Flow, FacFlow, JcFlow, FacPress, JcPress, Ds2Flow) VALUES (${msg.payload.ProgramNumber.value}, ${msg.payload.NumOfLeaks.value}, ${msg.payload.FsFlow.value}, ${msg.payload.MsFlow.value}, ${msg.payload.Ds1Flow.value}, ${msg.payload.SpruFlow.value}, ${msg.payload.Jc1Flow.value}, ${msg.payload.Jc2Flow.value}, ${msg.payload.Jc3Flow.value}, ${msg.payload.Jc4Flow.value}, ${msg.payload.FacFlow.value}, ${msg.payload.JcFlow.value}, ${msg.payload.FacPress.value}, ${msg.payload.JcPress.value}, ${msg.payload.Ds2Flow.value});`
return newMsg;

```

I would appreciate any help, I am still very new to Javascript and Nodered

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 March 2021 15:09 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/2 "2021-03-24T15:09:00Z")

</div>

You can test a value for undefined using, for example,  
`if (typeof msg.payload.whatever === "undefined")`  
so if you want to set it to zero then  
`if (typeof msg.payload.whatever === "undefined") msg.payload.whatever = 0`

However I am not sure that is what you would want.  
If you configure the Join node correctly then values from last time should still be there even if they have not changed.

---

<div class="post-metadata">

**Author:** ![Dgillett](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@Dgillett](https://discourse.nodered.org/u/Dgillett)\
**Post date:** [24 March 2021 16:25 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/3 "2021-03-24T16:25:03Z")

</div>

That may be exactly what I need, I want the undefined values to equal zero. I try and se what happens and update the result

---

<div class="post-metadata">

**Author:** ![Dgillett](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@Dgillett](https://discourse.nodered.org/u/Dgillett)\
**Post date:** [24 March 2021 16:35 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/4 "2021-03-24T16:35:37Z")

</div>

```auto
var newMsg = { payload: msg.payload };

if (typeof msg.payload.ProgramNumber === "undefined") msg.payload.ProgramNumber = 0;
if (typeof msg.payload.NumOfLeaks === "undefined") msg.payload.NumOfLeaks = 0;
if (typeof msg.payload.FsFlow === "undefined") msg.payload.FsFlow = 0;
if (typeof msg.payload.MsFlow === "undefined") msg.payload.MsFlow = 0;
if (typeof msg.payload.Ds1Flow === "undefined") msg.payload.Ds1Flow = 0;
if (typeof msg.payload.SpruFlow === "undefined") msg.payload.SpruFlow = 0;
if (typeof msg.payload.Jc1Flow === "undefined") msg.payload.Jc1Flow = 0;
if (typeof msg.payload.Jc2Flow === "undefined") msg.payload.Jc2Flow = 0;
if (typeof msg.payload.Jc3Flow === "undefined") msg.payload.Jc3Flow = 0;
if (typeof msg.payload.Jc4Flow === "undefined") msg.payload.Jc4Flow = 0;
if (typeof msg.payload.FacFlow === "undefined") msg.payload.FacFlow = 0;
if (typeof msg.payload.JcFlow === "undefined") msg.payload.JcFlow = 0;
if (typeof msg.payload.FacPress === "undefined") msg.payload.FacPress = 0;
if (typeof msg.payload.JcPress === "undefined") msg.payload.JcPress = 0;
if (typeof msg.payload.Ds2Flow === "undefined") msg.payload.Ds2Flow = 0;

newMsg.topic = `INSERT INTO SSreadings (ProgramNumber, NumOfLeaks, FsFlow, MsFlow, Ds1Flow, SpruFlow, Jc1Flow, Jc2Flow, Jc3Flow, Jc4Flow, FacFlow, JcFlow, FacPress, JcPress, Ds2Flow) VALUES (${msg.payload.ProgramNumber.value}, ${msg.payload.NumOfLeaks.value}, ${msg.payload.FsFlow.value}, ${msg.payload.MsFlow.value}, ${msg.payload.Ds1Flow.value}, ${msg.payload.SpruFlow.value}, ${msg.payload.Jc1Flow.value}, ${msg.payload.Jc2Flow.value}, ${msg.payload.Jc3Flow.value}, ${msg.payload.Jc4Flow.value}, ${msg.payload.FacFlow.value}, ${msg.payload.JcFlow.value}, ${msg.payload.FacPress.value}, ${msg.payload.JcPress.value}, ${msg.payload.Ds2Flow.value});`
return newMsg;

```

Is this correct when adding to my Original Script?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [24 March 2021 16:49 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/5 "2021-03-24T16:49:07Z")

</div>

No neeed to define all payload options, just loop through them

```auto
for(const K of msg.payload){
    if (typeof msg.payload[k] === "undefined"){
         msg.payload[k] = 0;
    }
}

```

---

<div class="post-metadata">

**Author:** ![Dgillett](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@Dgillett](https://discourse.nodered.org/u/Dgillett)\
**Post date:** [24 March 2021 16:53 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/6 "2021-03-24T16:53:26Z")

</div>

If you do not mind me asking what does the K stand for?

```auto
for(const K of msg.payload){
    if (typeof msg.payload[k] === "undefined"){
         msg.payload[k] = 0;

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [24 March 2021 16:54 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/7 "2021-03-24T16:54:22Z")

</div>

K is just a variable named K, i could have used any name i choose K for key

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 March 2021 16:55 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/8 "2021-03-24T16:55:46Z")

</div>

Your test and assignments to zero are changing the old msg.payload rather than the one in newMsg. However, since the line  
`var newMsg = { payload: msg.payload };`  
actually just makes the new payload be a reference to the original one then it should in fact work. Why are you creating a new message though? I would delete the first line, change the line  
`newMsg.topic = ...`  
to  
`msg.topic = ...`  
and change the last line to  
`return msg`

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 March 2021 16:57 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/9 "2021-03-24T16:57:35Z")

</div>

> [@E1cid](#):
>
> No neeed to define all payload options, just loop through them

Will that work? if `msg.payload.something` does not exist then I don't think the `for .. of` loop will find it.

---

<div class="post-metadata">

**Author:** ![Dgillett](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@Dgillett](https://discourse.nodered.org/u/Dgillett)\
**Post date:** [24 March 2021 17:04 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/10 "2021-03-24T17:04:00Z")

</div>

I just created a new message through some tutorials of javascript, trying to learn Javascript on the fly here, Im open to any suggestions for better and proper code.

```auto
if (typeof msg.payload.ProgramNumber === "undefined") msg.payload.ProgramNumber = 0;
if (typeof msg.payload.NumOfLeaks === "undefined") msg.payload.NumOfLeaks = 0;
if (typeof msg.payload.FsFlow === "undefined") msg.payload.FsFlow = 0;
if (typeof msg.payload.MsFlow === "undefined") msg.payload.MsFlow = 0;
if (typeof msg.payload.Ds1Flow === "undefined") msg.payload.Ds1Flow = 0;
if (typeof msg.payload.SpruFlow === "undefined") msg.payload.SpruFlow = 0;
if (typeof msg.payload.Jc1Flow === "undefined") msg.payload.Jc1Flow = 0;
if (typeof msg.payload.Jc2Flow === "undefined") msg.payload.Jc2Flow = 0;
if (typeof msg.payload.Jc3Flow === "undefined") msg.payload.Jc3Flow = 0;
if (typeof msg.payload.Jc4Flow === "undefined") msg.payload.Jc4Flow = 0;
if (typeof msg.payload.FacFlow === "undefined") msg.payload.FacFlow = 0;
if (typeof msg.payload.JcFlow === "undefined") msg.payload.JcFlow = 0;
if (typeof msg.payload.FacPress === "undefined") msg.payload.FacPress = 0;
if (typeof msg.payload.JcPress === "undefined") msg.payload.JcPress = 0;
if (typeof msg.payload.Ds2Flow === "undefined") msg.payload.Ds2Flow = 0;

msg.topic = `INSERT INTO SSreadings (ProgramNumber, NumOfLeaks, FsFlow, MsFlow, Ds1Flow, SpruFlow, Jc1Flow, Jc2Flow, Jc3Flow, Jc4Flow, FacFlow, JcFlow, FacPress, JcPress, Ds2Flow) VALUES (${msg.payload.ProgramNumber.value}, ${msg.payload.NumOfLeaks.value}, ${msg.payload.FsFlow.value}, ${msg.payload.MsFlow.value}, ${msg.payload.Ds1Flow.value}, ${msg.payload.SpruFlow.value}, ${msg.payload.Jc1Flow.value}, ${msg.payload.Jc2Flow.value}, ${msg.payload.Jc3Flow.value}, ${msg.payload.Jc4Flow.value}, ${msg.payload.FacFlow.value}, ${msg.payload.JcFlow.value}, ${msg.payload.FacPress.value}, ${msg.payload.JcPress.value}, ${msg.payload.Ds2Flow.value});`
return msg;

```

Does this look correct now?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 March 2021 17:04 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/11 "2021-03-24T17:04:56Z")

</div>

Put a debug node on the output and feed it some data and see if it works.

---

<div class="post-metadata">

**Author:** ![Dgillett](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@Dgillett](https://discourse.nodered.org/u/Dgillett)\
**Post date:** [24 March 2021 17:06 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/12 "2021-03-24T17:06:24Z")

</div>

I will and then send the results back, Thanks

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [24 March 2021 17:13 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/13 "2021-03-24T17:13:10Z")

</div>

You are correct, was not thinking clearly.  
then i would define an array of keys and check them

```auto
const keys = ["ProgramNumber",
    "NumOfLeaks",
    "FsFlow",
    "MsFlow",
    "Ds1Flow",
    "SpruFlow",
    "Jc1Flow",
    "Jc2Flow",
    "Jc3Flow",
    "Jc4Flow",
    "FacFlow",
    "JcFlow",
    "FacPress",
    "JcPress",
    "Ds2Flow"]
for(const V of keys){
    if (typeof msg.payload[V] === "undefined"){
         msg.payload[V] = {"value": 0};
    }
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 March 2021 17:18 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/14 "2021-03-24T17:18:44Z")

</div>

If you are using nodejs 14 you can take advantage of optional chaining.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8aa1b6424eb8b346a55b155fc69c3c58b969e217.png)

e.g..  
`msg.topic = `INSERT INTO SSreadings (ProgramNumber, NumOfLeaks, FsFlow, MsFlow, Ds1Flow, SpruFlow, Jc1Flow, Jc2Flow, Jc3Flow, Jc4Flow, FacFlow, JcFlow, FacPress, JcPress, Ds2Flow) VALUES (${msg.payload?.ProgramNumber?.value || 0}, ${msg.payload?.NumOfLeaks?.value || 0}, etc etc` `

---

<div class="post-metadata">

**Author:** ![Dgillett](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@Dgillett](https://discourse.nodered.org/u/Dgillett)\
**Post date:** [24 March 2021 17:20 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/15 "2021-03-24T17:20:59Z")

</div>

![Capture2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/6/568efdb7f9eab5f318749c0f72d23e7678e613f7.png)

The Zeros are taking the place of Undefined now, but im getting this new error as shown in the pic above, any Ideas

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [24 March 2021 17:27 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/16 "2021-03-24T17:27:28Z")

</div>

> [@Dgillett](#):
>
> ```auto
> if (typeof msg.payload.Ds2Flow === "undefined") msg.payload.Ds2Flow = 0;
> 
> ```

is not setting the value property

```auto
if (typeof msg.payload.Ds2Flow === "undefined") msg.payload.Ds2Flow = {"value": 0};

```

---

<div class="post-metadata">

**Author:** ![Dgillett](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@Dgillett](https://discourse.nodered.org/u/Dgillett)\
**Post date:** [24 March 2021 17:36 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/17 "2021-03-24T17:36:55Z")

</div>

```auto
if (typeof msg.payload.ProgramNumber === "undefined") msg.payload.ProgramNumber.value = {"value":0};
if (typeof msg.payload.NumOfLeaks === "undefined") msg.payload.NumOfLeaks.value = {"value":0};
if (typeof msg.payload.FsFlow === "undefined") msg.payload.FsFlow.value = {"value":0};
if (typeof msg.payload.MsFlow === "undefined") msg.payload.MsFlow.value = {"value":0};
if (typeof msg.payload.Ds1Flow === "undefined") msg.payload.Ds1Flow.vaue = {"value":0};
if (typeof msg.payload.SpruFlow === "undefined") msg.payload.SpruFlow.value = {"value":0};
if (typeof msg.payload.Jc1Flow === "undefined") msg.payload.Jc1Flow.value = {"value":0};
if (typeof msg.payload.Jc2Flow === "undefined") msg.payload.Jc2Flow.value = {"value":0};
if (typeof msg.payload.Jc3Flow === "undefined") msg.payload.Jc3Flow.value = {"value":0};
if (typeof msg.payload.Jc4Flow === "undefined") msg.payload.Jc4Flow.value = {"value":0};
if (typeof msg.payload.FacFlow === "undefined") msg.payload.FacFlow.value = {"value":0};
if (typeof msg.payload.JcFlow === "undefined") msg.payload.JcFlow.value = {"value":0};
if (typeof msg.payload.FacPress === "undefined") msg.payload.FacPress.value = {"value":0};
if (typeof msg.payload.JcPress === "undefined") msg.payload.JcPress.value = {"value":0};
if (typeof msg.payload.Ds2Flow === "undefined") msg.payload.Ds2Flow.value = {"value":0};

msg.topic = `INSERT INTO SSreadings (ProgramNumber, NumOfLeaks, FsFlow, MsFlow, Ds1Flow, SpruFlow, Jc1Flow, Jc2Flow, Jc3Flow, Jc4Flow, FacFlow, JcFlow, FacPress, JcPress, Ds2Flow) VALUES (${msg.payload.ProgramNumber.value}, ${msg.payload.NumOfLeaks.value}, ${msg.payload.FsFlow.value}, ${msg.payload.MsFlow.value}, ${msg.payload.Ds1Flow.value}, ${msg.payload.SpruFlow.value}, ${msg.payload.Jc1Flow.value}, ${msg.payload.Jc2Flow.value}, ${msg.payload.Jc3Flow.value}, ${msg.payload.Jc4Flow.value}, ${msg.payload.FacFlow.value}, ${msg.payload.JcFlow.value}, ${msg.payload.FacPress.value}, ${msg.payload.JcPress.value}, ${msg.payload.Ds2Flow.value});`
return msg;

```

This Look Correct? When Doing this I receive the original error when doing this

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 March 2021 17:42 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/18 "2021-03-24T17:42:17Z")

</div>

That is because you are testing, for example, `msg.payload.NumOfLeaks` and setting that to zero, but the value in the sql comes from `msg.payload.NumOfLeaks.value` which is still undefined.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [24 March 2021 17:46 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/19 "2021-03-24T17:46:04Z")

</div>

remove the .value from each line

```auto
if (typeof msg.payload.Ds2Flow === "undefined") msg.payload.Ds2Flow = {"value":0};

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 March 2021 17:49 UTC](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121/20 "2021-03-24T17:49:52Z")

</div>

> [@E1cid](#):
>
> remove the .value from each line

That rather depends on whether the number is in `msg.payload.NumOfLeaks` or `msg.payload.NumOfLeaks.value`

[Next page](https://discourse.nodered.org/t/help-with-javascript-on-function-node/43121.md?page=2)
