# Undefs don't want to go away

**URL:** https://discourse.nodered.org/t/undefs-dont-want-to-go-away/68541
**Category:** General
**Created:** [4 October 2022 11:22 UTC](https://discourse.nodered.org/t/undefs-dont-want-to-go-away/68541 "2022-10-04T11:22:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![SeRiusRod](https://avatars.discourse-cdn.com/v4/letter/s/278dde/32.png) [@SeRiusRod](https://discourse.nodered.org/u/SeRiusRod)
#### Post date: [4 October 2022 11:22 UTC](https://discourse.nodered.org/t/undefs-dont-want-to-go-away/68541/1 "2022-10-04T11:22:25Z")

</div>

I have an strange problem and I thought I could share it in case someone wants to burn some neurons.

I have I flow section where I retrieve data from three modbus queries and build an unique message. For that I use a join node. In the end this json I build with the data is published to mqtt and stored in context.

 ![imagen](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/b/5bd35cc86d65f72e562573322cff4696f06bad81.png)

The modbus is not so stable, so sometimes one part is missing and I get undefined values. So I inserted that function node:

```javascript
if (!msg.payload.hasOwnProperty("power") || msg.payload.power == undefined) {
    msg.payload.power = global.get("pv_sources.charger.power") || 0;
    node.warn("Warning: Power undefined!");
}

if (!msg.payload.hasOwnProperty("voltage") || msg.payload.voltage == undefined)
    msg.payload.voltage = global.get("pv_sources.charger.voltage") || 0;

if (!msg.payload.hasOwnProperty("energy_kwh") || msg.payload.energy_kwh == undefined)
    msg.payload.energy_kwh = global.get("pv_sources.charger.energy_kwh") || 0;

if (!msg.payload.hasOwnProperty("energy_daily_kwh") || msg.payload.energy_daily_kwh == undefined)
    msg.payload.energy_daily_kwh = global.get("pv_sources.charger.energy_daily_kwh") || 0;

return msg;

```

The debug node sometimes registers the undefined value but the next line shows that is corrected.  
 ![imagen](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/233e6d7a483d296a4d0ec3b6cd7df19de9cbcf04.png)

After that, the whole payload is stored con context and sent to mqtt.  
 ![imagen](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fc5b80f62bbe30984a1250b80745d67f235dc870.png)

Now comes the strange thing... The mqtt broker is showing messages without those values that are fixed on the function.  
 ![imagen](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/87a655f1614de1dda27dc6b16dc0095f3440d301.png)  
Or  
 ![imagen](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/87ac1cac0644b33392c0bbf9b289a44c1a88fb33.png)

Strange, isn't it?  
PS: Buffer nodes reply power, current / voltage / energy\_kwh, energy\_daily\_kwh  
Mppt node has a jsonata line that builds the object shown in the context image, and then there's a node that sends it mqtt. No more data changes.

---

<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: [4 October 2022 12:26 UTC](https://discourse.nodered.org/t/undefs-dont-want-to-go-away/68541/2 "2022-10-04T12:26:48Z")

</div>

> [@SeRiusRod](#):
>
> Mppt node has a jsonata line that builds the object shown in the context image, and then there's a node that sends it mqtt. No more data changes.

Since your Undefs function fills out any missing properties, it can only be something to do with either...

- `msg.payload.energy_daily_kwh` is an empty string, 0, false or something else != `undefined`
  - i.e. the `if(msg.payload.energy_daily_kwh == undefined)` fails and `msg.payload.energy_daily_kwh = global.get("pv_sources.charger.energy_daily_kwh") || 0;` is not being called.
  - you can use a function to better determine this (SEE BELOW)

- the node named `mppt` is doing something odd
- there is a set by reference issue somewhere
  - these are solved by deep cloning the msg to ensure it cannot be changed by reference in another node.

* * *

Better test...

As alluded to before, `0`, `''` and `false` will fail to call your `global.get()` ...

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

You could change it to ...

```auto
if (!isNumeric(msg.payload.power))
    msg.payload.power = global.get("pv_sources.charger.power") || 0;

if (!isNumeric(msg.payload.voltage))
    msg.payload.voltage = global.get("pv_sources.charger.voltage") || 0;

if (!isNumeric(msg.payload.energy_kwh))
    msg.payload.energy_kwh = global.get("pv_sources.charger.energy_kwh") || 0;

if (!isNumeric(msg.payload.energy_daily_kwh))
    msg.payload.energy_daily_kwh = global.get("pv_sources.charger.energy_daily_kwh") || 0;

function isNumeric(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

return msg;

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [3 December 2022 12:27 UTC](https://discourse.nodered.org/t/undefs-dont-want-to-go-away/68541/3 "2022-12-03T12:27:23Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
