# 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:** 20\
**Page:** 1

<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 14:27 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/1 "2020-06-25T14:27:21Z")

</div>

Been at this for a few hours and have plumbed the depths of these forums, but I am still stuck.....

Doing a select on a MySQL database and if some of the results are missing from the database, I get a TypeError undefined.... so I need to check for it and simply replace the missing bits of data with something else (in my case "" will do).

Here are the tests I have used to try and detect the 'undefined' case;

```auto
//if (typeof msg.payload.type === undefined) {
//if (typeof msg.payload.type === 'undefined') {
//if (msg.payload.type === undefined || msg.payload.type === null) {
///if (msg.payload.type.length === 0) {

```

I don't think I can post my flow here, since the database is needed.  
So here is a screen shot of it working and not working.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0c47d52eb0ee27233b51cbafcb9deb41d95aa585.png)

The first two debug messages are the database returning everything.  
The bottom two debug messages are the database not returning everything.

It seems that the error is happening BEFORE my code gets a chance at testing for it....

Put simply, how can I test for 'undefined' in this situation?

---

<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 15:16 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/2 "2020-06-25T15:16:45Z")

</div>

Could you not use -

```auto
Type: msg.payload[0].type || "",

```

---

<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 15:19 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/3 "2020-06-25T15:19:57Z")

</div>

Thanks for the reply, unfortunately it does not detect the undefined condition either.

I have added that test to comment section of tried, but not working.

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

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [25 June 2020 15:24 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/4 "2020-06-25T15:24:10Z")

</div>

Am I understanding correctly that you're looking at a situation where you have an object, and you're hoping to test whether the object has a certain key?  
If so, have you tried for `msg.payload.hasOwnProperty('type')`?

> **[Object.prototype.hasOwnProperty()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty)**
>
> The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).

---

<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 15:27 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/5 "2020-06-25T15:27:56Z")

</div>

Interesting. I had not come across that check before....  
Here is a screen shot to make sure I have implemented it correctly.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/f/9f34c4fc8feb61dd1c956652434bb879311ba2c8.png)

If it is correct syntax, it does not work, still getting the undefined error.

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [25 June 2020 15:31 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/6 "2020-06-25T15:31:09Z")

</div>

I missed a `[0]` in your screenshot. What it does is it checks with the object you call it on (in my example above on `msg.payload`) for the existence of a property (or a key, if you will) named `'type'`. So instead, try it with `msg.payload[0].hasOwnProperty('type')`

---

<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 15:36 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/7 "2020-06-25T15:36:52Z")

</div>

If it still isn't working feed the message you are trying to test into a debug node and show us what the message contains. I don't think you have done that so far, or if you did I did not understand what you were showing.

---

<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 15:42 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/8 "2020-06-25T15:42:32Z")

</div>

Your original debug screenshot does not show msg.payload as an array.

---

<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 15:45 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/9 "2020-06-25T15:45:34Z")

</div>

Getting a different error now.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/5/158a471c49cff02cd316f1405e9ba71bf229ed65.png)

Here is the flow.

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

The debug node does not show anything when it fails.  
Only when the select statement works.

As I said in my first post, it seems that the 'undefined' error is happening before my code, so that's why none of the usual tests are working?

---

<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 15:49 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/10 "2020-06-25T15:49:16Z")

</div>

Put the debug node before your function so you can see what is going _into_ the function.

---

<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 15:52 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/11 "2020-06-25T15:52:13Z")

</div>

I assume this is what you mean?

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

The select statement is going into the function correctly.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/0/10e2c73206e1c92a2be73c8761faf40ef6eabe2a.png)

Since that aircraft is not in the database, it comes back empty.

---

<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 15:55 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/12 "2020-06-25T15:55:36Z")

</div>

OK, the payload is an empty array. So in your function you can use something like  
`if (msg.payload.length > 0) {`  
Whenever you have a node which is not doing what you expect the first thing to do is to check what is going into it so you know what you are dealing with.

---

<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 15:59 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/13 "2020-06-25T15:59:00Z")

</div>

Thanks for your help.  
As you can see from the comments mounting up at the top of the block, I have tried testing for a length of none.

Assuming I have the syntax correct, this also fails to detect the undefined condition.

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

---

<div class="post-metadata">

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

</div>

Slight correction, you have tested for `msg.payload.type.length` to be 0. Not for the length of `msg.payload`. Testing for `type.length` would have for the number of characters, or if it was an array for the number of elements in the array, for `msg.payload.type`.

---

<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:05 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/15 "2020-06-25T16:05:03Z")

</div>

You are correct, my bad for not including it after testing it.

Here are the tests I have tried;

```auto
//if (typeof(msg.payload.type) === undefined) {
//if (typeof msg.payload.type === 'undefined') {
//if (msg.payload.type === undefined || msg.payload.type === null) {
//if (msg.payload.type.length === 0) {
//if (msg.payload.length === 0) {
//if (msg.payload.type || "") {
//if (msg.payload[0].type || "") {
//if (msg.payload.hasOwnProperty('type')) {
//if (msg.payload.length > 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:** [25 June 2020 16:07 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/16 "2020-06-25T16:07:13Z")

</div>

Please post the full function, with the msg.payload.length test so we can see what else is going on in the function. Copy the source please, go to a new line and click the `</>` button and paste it in there.

---

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

</div>

Here is the full code in the function block.

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

node.warn("Raw "+ msg.payload);
//if (typeof(msg.payload.type) === undefined) {
//if (typeof msg.payload.type === 'undefined') {
//if (msg.payload.type === undefined || msg.payload.type === null) {
//if (msg.payload.type.length === 0) {
//if (msg.payload.length === 0) {
//if (msg.payload.type || "") {
//if (msg.payload[0].type || "") {
//if (msg.payload.hasOwnProperty('type')) {
//if (msg.payload.length > 0) {

if (msg.payload.length > 0) {
   msg.payload = {
            Timestamp: msg.dts,
            Flight: msg.flight,
            Tail: msg.tail,
            Type: "blank",
            Desc: msg.payload[0].description,
            ICAOtxt: msg.aes,
            ICAO: msg.aes = "https://tar1090.adsbexchange.com/?icao="+ msg.aes,
            Raw : msg.raw
}
} else {
   msg.payload = {
            Timestamp: msg.dts,
            Flight: msg.flight,
            Tail: msg.tail,
            Type: msg.payload[0].type,
            Desc: 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:** ![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:09 UTC](https://discourse.nodered.org/t/trouble-testing-for-undefined/28973/18 "2020-06-25T16:09:11Z")

</div>

If array is empty msg.payload[0].description will not be defined also.

---

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

</div>

Sometimes correct.  
Some aircraft have types and descriptions and some have different combinations.  
Since I never get to test for descriptions, I have not worried about that check yet... Once I get the missing 'type' I can work on any missing 'descriptions'.

---

<div class="post-metadata">

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

</div>

> [@thebaldgeek](#):
>
> `node.warn("Raw "+ msg.payload);`

This line will cause a string concatenation to happen, it will take `msg.payload` and get the `toString()` version of it and adds it to the string `"Raw "`. If instead you were to use it for debugging, try warning directly with `msg.payload`. When you see `Raw [object object]` in the debug sidebar, it's because msg.payload is an object, and toString of an object results in the string representation of [Object object].

Furthermore, this line `ICAO: msg.aes = "https://tar1090.adsbexchange.com/?icao="+ msg.aes,` will likely not do what you think it will.

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