# How to test for undefined in function node

**URL:** <https://discourse.nodered.org/t/how-to-test-for-undefined-in-function-node/19200>\
**Category:** General\
**Created:** [15 December 2019 20:54 UTC](https://discourse.nodered.org/t/how-to-test-for-undefined-in-function-node/19200 "2019-12-15T20:54:06Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [15 December 2019 20:54 UTC](https://discourse.nodered.org/t/how-to-test-for-undefined-in-function-node/19200/1 "2019-12-15T20:54:06Z")

</div>

I have a number of messages which share the same topic but with various payload objects, which vary from message to message.

The code bellow does what I need when the relevant object is present in the message

var1 = {payload: msg.payload.S29cmnd\_D15.STATE};  
return var1;

I would like to know how to avoid getting this error "TypeError: Cannot read property 'STATE' of undefined" if that object is not in the message.

Basically I think I need to "test" if the object is there before trying to assign it, but cannot figure out the correct code.

Many Thanks...

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [15 December 2019 20:58 UTC](https://discourse.nodered.org/t/how-to-test-for-undefined-in-function-node/19200/2 "2019-12-15T20:58:18Z")

</div>

A much used method I've seen used a lot is to use this type of construct

`var1 = something || 0;`

Then if something doesn't exist - var1 will be assigned 0

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [15 December 2019 21:05 UTC](https://discourse.nodered.org/t/how-to-test-for-undefined-in-function-node/19200/3 "2019-12-15T21:05:18Z")

</div>

Hi @Sean-McG

You can check if it exists using something like:

```auto
if (msg.payload.hasOwnProperty("S29cmnd_D15")) {
   msg.payload = msg.payload.S29cmnd_D15.STATE;
   return msg;
} else {
  // do something else because it does not exist
}

```

Note how this code updates and returns the existing `msg` object, rather than create a new message object - that's generally the best practise to follow.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [16 December 2019 11:51 UTC](https://discourse.nodered.org/t/how-to-test-for-undefined-in-function-node/19200/4 "2019-12-16T11:51:40Z")

</div>

Many thanks, looks simple once you know how, keep up the good work

---

<div class="post-metadata">

**Author:** ![marcusone](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@marcusone](https://discourse.nodered.org/u/marcusone)\
**Post date:** [16 December 2019 12:24 UTC](https://discourse.nodered.org/t/how-to-test-for-undefined-in-function-node/19200/5 "2019-12-16T12:24:30Z")

</div>

Searching google for methods in JavaScript to do this will show you many ways and the pro/cons of each.

the `hasOwnProperty` works, however, last time i searched the safest/"better" method is to do.

```
if(typeof msg.payload.S29cmnd_D15 !== 'undefined') {
    // blah blah
} else {
    // more blah
}
```

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [16 December 2019 17:42 UTC](https://discourse.nodered.org/t/how-to-test-for-undefined-in-function-node/19200/6 "2019-12-16T17:42:57Z")

</div>

> [@Sean-McG](#):
>
> I would like to know how to avoid getting this error "TypeError: Cannot read property 'STATE' of undefined" if that object is not in the message.

If you just want to switch the payload to some element buried in a JS Object (or maybe not!), it's probably easiest/safest to use the JSONata expression shown below:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/df1fe291dad3bc3f81cf4fafb89fe6745c8f210e.png)

The JSONata expression parser stops as soon as it hits an undefined element in the "path", since any elements after that point are going to be undefined as well.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [19 December 2019 17:49 UTC](https://discourse.nodered.org/t/how-to-test-for-undefined-in-function-node/19200/7 "2019-12-19T17:49:15Z")

</div>

> [@knolleary](#):
>
> (msg.payload.hasOwnProperty("S29cmnd\_D15"))

Taking this message

object  
topic: "stat/alarm/RESULT"  
payload: object  
Time: "2019-12-19T17:09:21"  
MCP230XX\_INT: object  
D0: 1  
MS: 2075

I tried the following but this doesn't seem to work any ideas please ?

if (msg.payload.hasOwnProperty("MCP230XX\_INT.D0")) {  
msg.payload = msg.payload.MCP230XX\_INT.D0  
msg.topic = "0"  
}

return msg;

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [19 December 2019 18:12 UTC](https://discourse.nodered.org/t/how-to-test-for-undefined-in-function-node/19200/8 "2019-12-19T18:12:32Z")

</div>

> [@Sean-McG](#):
>
> if (msg.payload.hasOwnProperty("MCP230XX\_INT.D0")) {  
> msg.payload = msg.payload.MCP230XX\_INT.D0  
> msg.topic = "0"  
> }

Maybe its needs

```auto
if (msg.payload.hasOwnProperty("MCP230XX_INT")) {
   if (msg.payload.MCP230XX_INT.hasOwnProperty("D0")) {
       msg.payload = msg.payload.MCP230XX_INT.D0
       msg.topic = "0"
   }
}

```

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [19 December 2019 18:14 UTC](https://discourse.nodered.org/t/how-to-test-for-undefined-in-function-node/19200/9 "2019-12-19T18:14:49Z")

</div>

> [@cymplecy](#):
>
> if (msg.payload.hasOwnProperty("MCP230XX\_INT")) { if (msg.payload.MCP230XX\_INT.hasOwnProperty("D0")) { msg.payload = msg.payload.MCP230XX\_INT.D0 msg.topic = "0" } }

That did the trick thanks a lot 😉

---

<div class="post-metadata">

**Author:** ![rei\_vilo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rei_vilo/32/162_2.png) [@rei\_vilo](https://discourse.nodered.org/u/rei_vilo)\
**Post date:** [9 March 2020 18:53 UTC](https://discourse.nodered.org/t/how-to-test-for-undefined-in-function-node/19200/10 "2020-03-09T18:53:03Z")

</div>

[knolleary](https://discourse.nodered.org/t/how-to-test-for-undefined-in-function-node/19200/3) Thank you for the solution.

I investigated whether it was possible to use a JSONata expression and found [$exits()](https://docs.jsonata.org/boolean-functions#exists).

 ![Capture 2020-03-09 à 19.44.27 copie](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/c/4cb282df6e1861d76c6814f80d856a46fb2de9da.png)

So a function node with

```auto
if (msg.hasOwnProperty("motor"))
{
    return msg;
}   

```

is similar to a switch node with JSONata expression set to

```auto
$exists(msg.motor)

```

Now,

- Wouldn't be nice to get an additional option on the switch node with **exists** on the drop-down menu and the name of the field on the property?

 ![Capture 2020-03-09 à 19.54.53](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/6/a6ec33a2823be06e8a3f6527512d6c54f3345f8a.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [9 March 2020 19:11 UTC](https://discourse.nodered.org/t/how-to-test-for-undefined-in-function-node/19200/11 "2020-03-09T19:11:33Z")

</div>

With the Switch node you can use the `is not null` option that is already there:

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

That works for top level properties.

Since NR 1.0 there is also the `has key` option that can be used to check if a key exists on an object property - but can't be used on the top level object itself.

---

<div class="post-metadata">

**Author:** ![rei\_vilo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rei_vilo/32/162_2.png) [@rei\_vilo](https://discourse.nodered.org/u/rei_vilo)\
**Post date:** [9 March 2020 20:52 UTC](https://discourse.nodered.org/t/how-to-test-for-undefined-in-function-node/19200/12 "2020-03-09T20:52:00Z")

</div>

Thank you! So we have 4 different solutions now 🙂

---

<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:** [23 March 2020 20:52 UTC](https://discourse.nodered.org/t/how-to-test-for-undefined-in-function-node/19200/13 "2020-03-23T20:52:06Z")

</div>

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