# How check undefined msg in a function node?

**URL:** <https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263>\
**Category:** General\
**Created:** [20 September 2021 03:02 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263 "2021-09-20T03:02:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![essereumano](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/essereumano/32/48262_2.png) [@essereumano](https://discourse.nodered.org/u/essereumano)\
**Post date:** [20 September 2021 03:02 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/1 "2021-09-20T03:02:25Z")

</div>

Hi again everyone, this time I'm going crazy with the function node.  
To understand which message arrived (each has a different name and arrives at different times) I tried various ways but seems they are wrong:

```auto
if (!msg.hasOwnProperty ('payload [0].r_id'))
if (msg.payload [0].r_id != undefined)
if (typeof msg.payload[0].r_id != "undefined")

```

In both cases the function gives me the error: TypeError: Cannot read property 'r\_id' of undefined

How can I check if an item is not declared?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [20 September 2021 06:33 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/2 "2021-09-20T06:33:53Z")

</div>

It is saying that it can't find property r\_id as payload[0] isn't defined, so you need to check at that level. Eg. if (msg.payload[0] != undefined)

---

<div class="post-metadata">

**Author:** ![essereumano](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/essereumano/32/48262_2.png) [@essereumano](https://discourse.nodered.org/u/essereumano)\
**Post date:** [20 September 2021 06:37 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/3 "2021-09-20T06:37:55Z")

</div>

Thank you @dceejay , I will try this 😄

---

<div class="post-metadata">

**Author:** ![essereumano](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/essereumano/32/48262_2.png) [@essereumano](https://discourse.nodered.org/u/essereumano)\
**Post date:** [20 September 2021 06:50 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/4 "2021-09-20T06:50:15Z")

</div>

I tried your solution, the problem is that some times msg.payload [0] is defined but the r\_id property is not, in that case the code is executed even if it shouldn't. How can I also check r\_id?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [20 September 2021 06:56 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/5 "2021-09-20T06:56:55Z")

</div>

Did you try nesting the tests?

```auto
 if (msg.payload[0] != undefined) {
   if (msg.payload [0].r_id != undefined) {
      do something
   }
}

```

---

<div class="post-metadata">

**Author:** ![essereumano](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/essereumano/32/48262_2.png) [@essereumano](https://discourse.nodered.org/u/essereumano)\
**Post date:** [20 September 2021 06:59 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/6 "2021-09-20T06:59:58Z")

</div>

@zenofmud yes, i tried but the error is coming when msg.payload[0] is undefined and not when have a value. I don't find an all in one solution.

To be clearer I enter the full code:

```auto
if (msg.payload[0] != "undefined"){

   if (msg.payload[0].r_id > 0 && msg.payload[0].r_id != "undefined"){

        let formatobject = [{ "Nessuno": 0 }];

        for (let i = 0; i < msg.payload.length; i++) {

            let tempnome = msg.payload[i].r_nome;

            let tempid = msg.payload[i].r_id;

            let tempobject = {};

            tempobject[tempnome] = tempid;

            formatobject.push(tempobject);

        }

        msg1.options = formatobject;

        node.send([msg1, null, null, null, null, null, null]);

   }

}

```

---

<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:** [20 September 2021 07:09 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/7 "2021-09-20T07:09:28Z")

</div>

You have to test whether it is undefined _before_ you test for \>0. Swap the two round in the test.

---

<div class="post-metadata">

**Author:** ![essereumano](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/essereumano/32/48262_2.png) [@essereumano](https://discourse.nodered.org/u/essereumano)\
**Post date:** [20 September 2021 07:10 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/8 "2021-09-20T07:10:30Z")

</div>

Ok I try thank you

---

<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:** [20 September 2021 07:14 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/9 "2021-09-20T07:14:17Z")

</div>

It's undefined not "undefined"  
`if (msg.payload[0] != undefined && msg.payload[0].r_id != undefined && msg.payload[0].r_id > 0){`

---

<div class="post-metadata">

**Author:** ![essereumano](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/essereumano/32/48262_2.png) [@essereumano](https://discourse.nodered.org/u/essereumano)\
**Post date:** [20 September 2021 07:21 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/10 "2021-09-20T07:21:13Z")

</div>

You are true it's a refuse when I try typeof method, I've try @Colin and @E1cid solution but the error still remain

---

<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:** [20 September 2021 07:25 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/11 "2021-09-20T07:25:28Z")

</div>

so what is the incoming msg to the function, when you get the type error?

---

<div class="post-metadata">

**Author:** ![essereumano](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/essereumano/32/48262_2.png) [@essereumano](https://discourse.nodered.org/u/essereumano)\
**Post date:** [20 September 2021 07:32 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/12 "2021-09-20T07:32:24Z")

</div>

I try this code and delete msg.payload with change node, the error now is: TypeError: Cannot read property '0' of undefined

```auto
if (msg.payload != undefined){

    if (msg.payload[0] != undefined) {

        if (msg.payload[0].r_id != undefined){

            let formatobject = [{ "Nessuno": 0 }];

            for (let i = 0; i < msg.payload.length; i++) {

                let tempnome = msg.payload[i].r_nome;

                let tempid = msg.payload[i].r_id;

                let tempobject = {};

                tempobject[tempnome] = tempid;

                formatobject.push(tempobject);

            }

            msg1.options = formatobject;

            node.send([msg1, null, null, null, null, null, null]);

        }

   }

}

```

Before the modify msg.payload is 1 (number) and the error was TypeError: Cannot read property r\_id of undefined

---

<div class="post-metadata">

**Author:** ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)\
**Post date:** [20 September 2021 07:33 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/13 "2021-09-20T07:33:29Z")

</div>

Perhaps your payload does not exist at all or isn’t an array.

And by the way you should check “undefined” with fixed type `msg.payload !== undefined`

So it isn’t a array! To check this

```auto
if (typeof msg.payload === ‘array’)

```

> [@essereumano](#):
>
> msg.payload is 1 (number)

---

<div class="post-metadata">

**Author:** ![essereumano](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/essereumano/32/48262_2.png) [@essereumano](https://discourse.nodered.org/u/essereumano)\
**Post date:** [20 September 2021 07:38 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/14 "2021-09-20T07:38:24Z")

</div>

Yes payload not exist, and I get this error, another time payload exist and is an array but not have r\_id property so if i check only payload the code run when don't need.  
I'll try typeof thank you

---

<div class="post-metadata">

**Author:** ![essereumano](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/essereumano/32/48262_2.png) [@essereumano](https://discourse.nodered.org/u/essereumano)\
**Post date:** [20 September 2021 07:44 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/15 "2021-09-20T07:44:52Z")

</div>

I try your solution but the editor give me this error: This condition will always return 'false' since the types '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' and '"array"' have no overlap

---

<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:** [20 September 2021 07:45 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/16 "2021-09-20T07:45:15Z")

</div>

If you want to be certain...

```auto

if(Array.isArray(msg.payload) && msg.payload.length && typeof msg.payload[0] === "object" && typeof msg.payload[0].r_id === "number") {
  // msg.payload[0].r_id is definitely present & is definitely a number - now you can compare it to a number
} 

```

---

<div class="post-metadata">

**Author:** ![essereumano](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/essereumano/32/48262_2.png) [@essereumano](https://discourse.nodered.org/u/essereumano)\
**Post date:** [20 September 2021 07:50 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/17 "2021-09-20T07:50:11Z")

</div>

> [@Steve-Mcl](#):
>
> `if(Array.isArray(msg.payload) && msg.payload.length && typeof msg.payload[0] === "object" && typeof msg.payload[0].r_id === "number") {`

Thank you @Steve-Mcl , i try this but with msg,payload deleted I give TypeError: Cannot read property '0' of undefined  
and with msg.payload set to 1 the error still TypeError: Cannot read property r\_id of undefined

---

<div class="post-metadata">

**Author:** ![essereumano](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/essereumano/32/48262_2.png) [@essereumano](https://discourse.nodered.org/u/essereumano)\
**Post date:** [20 September 2021 07:54 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/18 "2021-09-20T07:54:27Z")

</div>

If you want I can post entire project but it's quite big... the problem is coming in the tab Notifiche  
when I select Centrale termica from Reparto dropdown [control\_room\_alpha.json](https://discourse.nodered.org/uploads/short-url/5zxRo7NWYUoxqTcjrgAnlneaNH9.json) (406,1 KB)

---

<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:** [20 September 2021 07:55 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/19 "2021-09-20T07:55:07Z")

</div>

> [@essereumano](#):
>
> i try this but with msg,payload deleted I give TypeError: Cannot read property '0' of undefined

Then something else is wrong because the `Array.isArray` test should fail & shortcut to `false`

Are you certain that error comes from this function? Is there any code BEFORE this `if` test?

---

<div class="post-metadata">

**Author:** ![essereumano](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/essereumano/32/48262_2.png) [@essereumano](https://discourse.nodered.org/u/essereumano)\
**Post date:** [20 September 2021 07:56 UTC](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263/20 "2021-09-20T07:56:21Z")

</div>

only code is new msg objects:

```auto
var msg1 = {};

var msg2 = {};

var msg3 = {};

var msg4 = {};

var msg5 = {};

var msg6 = {};

var msg7 = {};

```

This is the full code of function node:

```auto
var msg1 = {};

var msg2 = {};

var msg3 = {};

var msg4 = {};

var msg5 = {};

var msg6 = {};

var msg7 = {};

//popolazione dei dropdown e azzeramento input

if (Array.isArray(msg.payload) && msg.payload.length && typeof msg.payload[0] === "object" && typeof msg.payload[0].r_id === "number") {

            let formatobject = [{ "Nessuno": 0 }];

            for (let i = 0; i < msg.payload.length; i++) {

                let tempnome = msg.payload[i].r_nome;

                let tempid = msg.payload[i].r_id;

                let tempobject = {};

                tempobject[tempnome] = tempid;

                formatobject.push(tempobject);

            }

            msg1.options = formatobject;

            node.send([msg1, null, null, null, null, null, null]);

           

}

if (!msg.hasOwnProperty('payload[0].d_id')) {

    if (msg.payload[0].d_id > 0) {

        let formatobject = [{ "Nessuno": 0 }];

        for (let i = 0; i < msg.payload.length; i++) {

            let tempnome = msg.payload[i].d_nome;

            let tempid = msg.payload[i].d_id;

            let tempobject = {};

            tempobject[tempnome] = tempid;

            formatobject.push(tempobject);

        }

        msg2.options = formatobject;

        node.send([null, msg2, null, null, null, null, null]);

    }

}

if (!msg.hasOwnProperty('payload[0].io_id')) {

    if (msg.payload[0].io_id > 0) {

        let formatobject = [{ "Nessuno": 0 }];

        for (let i = 0; i < msg.payload.length; i++) {

            let tempnome = msg.payload[i].io_nome;

            let tempid = msg.payload[i].io_id;

            let tempobject = {};

            tempobject[tempnome] = tempid;

            formatobject.push(tempobject);

        }

        msg3.options = formatobject;

        node.send([null, null, msg3, null, null, null, null]);

    }

}

if (!msg.hasOwnProperty('payload[0].u_id')) {

    if (msg.payload[0].u_id > 0) {

        let formatobject = [{"Nessuno": 0 }];

        for (let i = 0; i < msg.payload.length; i++) {

            let tempnome = msg.payload[i].u_nome;

            let tempid = msg.payload[i].u_id;

            let tempobject = {};

            tempobject[tempnome] = tempid;

            formatobject.push(tempobject);

        }

        msg4.options = formatobject;

        node.send([null, null, null, msg4, null, null, null]);

    }

}

msg5.payload = "";

node.send([null, null, null, null, msg5, null, null]);

if (!msg.hasOwnProperty('payload[0].g_id')) {

    if (msg.payload[0].g_id > 0) {

        let formatobject = [{ "Nessuno": 0 }];

        for (let i = 0; i < msg.payload.length; i++) {

            let tempnome = msg.payload[i].g_nome;

            let tempid = msg.payload[i].g_id;

            let tempobject = {};

            tempobject[tempnome] = tempid;

            formatobject.push(tempobject);

        }

        msg6.options = formatobject;

        node.send([null, null, null, null, null, msg6, null]);

    }

}

msg7.payload = 0;

node.send([null, null, null, null, null, null, msg7]);

//abilitazione e disabilitazione dropdown in base alle scelte

if (msg.reparto > 0) {

    msg2.enabled = true;

    node.send([null, msg2, null, null, null, null, null]);

}

else{

    msg2.enabled = false;

    node.send([null, msg2, null, null, null, null, null]);

}

if (msg.dispositivo > 0) {

    msg3.enabled = true;

    node.send([null, null, msg3, null, null, null, null]);

}

else {

    msg3.enabled = false;

    node.send([null, null, msg3, null, null, null, null]);

}

if (msg.utente > 0) {

    msg6.enabled = false;

    node.send([null, null, null, null, null, msg6, null]);

}

else {

    msg6.enabled = true;

    node.send([null, null, null, null, null, msg6, null]);

}

if (msg.gruppo > 0) {

    msg4.enabled = false;

    node.send([null, null, null, msg4, null, null, null]);

}

else {

    msg4.enabled = true;

    node.send([null, null, null, msg4, null, null, null]);

}

return;

```

[Next page](https://discourse.nodered.org/t/how-check-undefined-msg-in-a-function-node/51263.md?page=2)
