# Help if not defined variable is accessed

**URL:** https://discourse.nodered.org/t/help-if-not-defined-variable-is-accessed/45377
**Category:** General
**Created:** [8 May 2021 23:27 UTC](https://discourse.nodered.org/t/help-if-not-defined-variable-is-accessed/45377 "2021-05-08T23:27:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [8 May 2021 23:27 UTC](https://discourse.nodered.org/t/help-if-not-defined-variable-is-accessed/45377/1 "2021-05-08T23:27:45Z")

</div>

Yeah, it is probably easy to do, but for now it escapes me how to do it.

This is the code on a `Function` node:

```auto
var m = msg.month;

m = parseInt(m);

var x = msg.table[m];

msg.payload = x;

return msg;

```

If `msg.table` isn't an array, it throws an error.

How do I get around it and use a _default_ value?

Thanks.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [8 May 2021 23:37 UTC](https://discourse.nodered.org/t/help-if-not-defined-variable-is-accessed/45377/2 "2021-05-08T23:37:51Z")

</div>

Ok, this seems to work:

```auto
var m = msg.month;

m = parseInt(m);
var x;

if (msg.table != undefined)
{
    x = msg.table[m];
} else
{
    x = 22;
}

msg.payload = x;

return msg;

```

If it isn't defined I get back `22`.

But is there a _neater_ way of doing it?

---

<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: [8 May 2021 23:44 UTC](https://discourse.nodered.org/t/help-if-not-defined-variable-is-accessed/45377/3 "2021-05-08T23:44:40Z")

</div>

For testing an array you can use `if(Array.isArray(msg.payload) && msg.payload.length)`  
This ensures the variable is an array && that it has 1 or more items.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [8 May 2021 23:50 UTC](https://discourse.nodered.org/t/help-if-not-defined-variable-is-accessed/45377/4 "2021-05-08T23:50:34Z")

</div>

Sorry, I don't understand that.

I can only resolve it to:

`if(msg.table.isArray()`

`msg.payload` isn't part of the equation as I see it.

`msg.month` will be set. That is set by the upstream node to set the month value for this month.

It is just the array `msg.table` which may not be set/defined and that is generated upstream too.

I may look at how it is set and do a condition there. But at the end of the day, it needs to be done somewhere and due to recent observations here is as good a place as any to check/test for it being set and if not, set a default value.  
Less nodes needed/used.

---

<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: [9 May 2021 00:02 UTC](https://discourse.nodered.org/t/help-if-not-defined-variable-is-accessed/45377/5 "2021-05-09T00:02:07Z")

</div>

I misread. But essentially is the same.

`if(Array.isArray(msg.table) && msg.table.length)`

The point is that testing for a variable being not undefined does not mean it is an array nor that it has 1 or more elements.

I.e. avoid bugs.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [9 May 2021 00:07 UTC](https://discourse.nodered.org/t/help-if-not-defined-variable-is-accessed/45377/6 "2021-05-09T00:07:38Z")

</div>

Yeah, thanks.

Alas (as you may have noticed/seen) I've been away from NR for a long time.  
(Other things happening)

I really do need to keep on top of checking if things I get are (or not) defined and deal with it better than throwing an error.

---

<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 May 2021 00:08 UTC](https://discourse.nodered.org/t/help-if-not-defined-variable-is-accessed/45377/7 "2021-05-23T00:08:21Z")

</div>

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