# Trouble testing for undefined

**URL:** <https://discourse.nodered.org/t/trouble-testing-for-undefined/28973>\
**Category:** General\
**Created:** [25 June 2020 14:27 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973 "2020-06-25T14:27:21Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![thebaldgeek](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thebaldgeek/32/93523_2.png) [@thebaldgeek](https://discourse.nodered.org/u/thebaldgeek)\
**Post date:** [25 June 2020 16:13 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/21 "2020-06-25T16:13:13Z")

</div>

Honestly, all my testing has been done without the 'node.warn', I just put that in there for this forums post thinking it would help show whats going on since the debug is of no value in this case.

The icao link works great. No problems with it at all.

---

<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:** [25 June 2020 16:17 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/22 "2020-06-25T16:17:37Z")

</div>

> [@thebaldgeek](#):
>
> Sometimes correct.

Always correct, if msg.payload.length is zero it will drop through to the else condtion, where you then use msg.payload[0].type, but msg.payload[0] does not exist, so in the else condition you cannot use anything in msg.payload because there is nothing there.

---

<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:** [25 June 2020 16:19 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/23 "2020-06-25T16:19:48Z")

</div>

Perhaps you need another test inside the first block of code (so there is something in the array). Having determined that there _is_ something there then you should be able to use the hasOwnProperty test to check whether type is present.

---

<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:** [25 June 2020 16:52 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/24 "2020-06-25T16:52:22Z")

</div>

You could try a ternary operator to check if the array element is true.

```auto
msg.dts = new Date().toUTCString();

msg.payload = {
            Timestamp: msg.dts,
            Flight: msg.flight,
            Tail: msg.tail,
            Type: (msg.payload[0] ? msg.payload[0].type : ""),
            Desc: (msg.payload[0] ? msg.payload[0].description : ""),
            ICAOtxt: msg.aes,
            ICAO: msg.aes = "https://tar1090.adsbexchange.com/?icao="+ msg.aes,
            Raw : msg.raw
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)\
**Post date:** [25 June 2020 17:10 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/25 "2020-06-25T17:10:49Z")

</div>

A general comment: In javascript, you often want to check if a property exists, for example you want to know if `msg.payload.prop1.subprop1.subsubprop1` exists.  
However, you cannot do `msg.payload.prop1.subprop1.hasOwnProperty('subsubprop1')`, if you cannot ensure that `msg.payload.prop1.subprop1` exists.  
Therefore, often you need to do things like  
`if(msg.hasOwnProperty('payload') && msg.payload.hasOwnProperty('prop1') && msg.payload.prop1.hasOwnProperty('subprop1'))` and so forth. If you have an array in the mix, you can check for `.length`, `Array.isArray()` etc.

This will be simplified with ECMAScript 2020, which includes the ?. syntax

Colin suggests the same, but I thought I give a more detailed explanation.

---

<div class="post-metadata">

**Author:** ![thebaldgeek](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thebaldgeek/32/93523_2.png) [@thebaldgeek](https://discourse.nodered.org/u/thebaldgeek)\
**Post date:** [25 June 2020 17:34 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/26 "2020-06-25T17:34:13Z")

</div>

@E1cid  
Fantastic!  
This is the only solution that is working.  
I have not heard of a 'ternary operator', but will be sure to look into it.

Thanks everyone for your ideas and checks.

---

<div class="post-metadata">

**Author:** ![hklages](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hklages/32/23899_2.png) [@hklages](https://discourse.nodered.org/u/hklages)\
**Post date:** [26 June 2020 06:57 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/28 "2020-06-26T06:57:10Z")

</div>

I use a small function:

`isValidProperty(msg,['payload', 'type']`

```auto
/** Validates whether property is safely accessable - empty string allowed
   * @param {object} nestdObj object
   * @param {array} path array with the property chain- should be non empty
   * @outputs {boolean} property exists
   */
  isValidProperty: (nestedObj, pathArray) => {
    const property = pathArray.reduce(
      (obj, key) => (obj && obj[key] !== 'undefined' ? obj[key] : undefined),
      nestedObj
    )
    return module.exports.isTruthy(property)
  },

```

isTruthy is here (to avoid infinite, etc - you can simplify that)

```auto
isTruthy: input => {
    return !(typeof input === 'undefined' || input === null ||
      (typeof input === 'number' && !Number.isFinite(input)))
  },

```

---

<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:** [10 July 2020 06:57 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/29 "2020-07-10T06:57:14Z")

</div>

Closed on 10 July 2020 06:57 UTC

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

[Previous page](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973.md?page=1)
