# Detecting "null" flow variables

**URL:** <https://discourse.nodered.org/t/detecting-null-flow-variables/14569>\
**Category:** General\
**Created:** [20 August 2019 21:18 UTC](https://discourse.nodered.org/t/detecting-null-flow-variables/14569 "2019-08-20T21:18:03Z")\
**Posts on this page:** 6\
**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:** [20 August 2019 21:18 UTC](https://discourse.nodered.org/t/detecting-null-flow-variables/14569/1 "2019-08-20T21:18:03Z")

</div>

I have this bit of code:

```auto
var w = flow.get("who_1") || {};
var p = msg.payload;
if (w.length > 0)
{
    // do stuff here.

```

The idea is that at/on first invocation the `flow.get("who_1")` will not be set.

I was under the impression that the `|| {}` implies that if there isn't a value, set it to null.  
(Don't know if there should be a space between the `{` and `}`.

The situation is that if the variable is not assigned, it needs to do the _do stuff here_ part.  
It isn't.

---

<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:** [20 August 2019 21:23 UTC](https://discourse.nodered.org/t/detecting-null-flow-variables/14569/2 "2019-08-20T21:23:09Z")

</div>

`variable === null` is the usual way of testing. A switch node is capable of it directly, `flow.variable` with rule “is null” or “is not null”

---

<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:** [20 August 2019 21:25 UTC](https://discourse.nodered.org/t/detecting-null-flow-variables/14569/3 "2019-08-20T21:25:53Z")

</div>

Alas because of a multitude of things, I am going with a `function` node rather than a lot of `switch` nodes one after the other.

It has been suggested I use the `FSM` node. But there are two I can see and both are old.  
I know nothing about them and reading their descriptions, it is a whole new world to learn if I want to use them.

---

<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:** [20 August 2019 21:28 UTC](https://discourse.nodered.org/t/detecting-null-flow-variables/14569/4 "2019-08-20T21:28:05Z")

</div>

This is the new code:

```auto
var w = flow.get("who_1")|| {};
var p = msg.payload;
if (w === null)
{
    //
    node.warn("1");
    return [msg,null];
} else
if (w == p)
{
    //
    node.warn("1");
    return [msg,null];
} else
node.warn("2");
return [null,msg];

```

with nothing being set in `flow.get('who_1)` I would hope that `1` is the output.  
Alas it isn't.  
I am getting `2` used and I am not seeing why.

---

<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:** [20 August 2019 21:51 UTC](https://discourse.nodered.org/t/detecting-null-flow-variables/14569/5 "2019-08-20T21:51:45Z")

</div>

> [@Trying\_to\_learn](#):
>
> I was under the impression that the `|| {}` implies that if there isn't a value, set it to null.

Not quite - and that is the source of the issue in your Function.

When you have statement like:

```auto
var A = B || C;

```

Then `A` is assigned the value of `B` if it has a true-like value. But if `B` has a 'false-like' value (`0`, `null`, `undefined`, `""`, `false`) then `A` is given the value of `C`.

So where you have `var w = flow.get("who_1")|| {};`, if the flow context value `who_1` is not defined, then `w` is given the value `{}` - which is an empty JavaScript object, _not_ `null`.

If you have not yet set a value in flow.who\_1, then the `flow.get()` statement will return `undefined`.

So the proper way to do the check you are doing is:

```auto
// don't use the ||{} trick as you want to know if it is undefined
var w = flow.get("who_1");
var p = msg.payload;
// Check if it is undefined, not null
if (w === undefined)
{
    //
    node.warn("1");
    return [msg,null];
} else if (w == p) {
    //
    node.warn("1");
    return [msg,null];
} else {
    node.warn("2");
}
return [null,msg];

```

---

<div class="post-metadata">

**Author:** ![Nikolay](https://avatars.discourse-cdn.com/v4/letter/n/a698b9/32.png) [@Nikolay](https://discourse.nodered.org/u/Nikolay)\
**Post date:** [1 April 2021 12:54 UTC](https://discourse.nodered.org/t/detecting-null-flow-variables/14569/6 "2021-04-01T12:54:34Z")

</div>

For me this worked as as the debug shows ''null'' and the default variable ''null'' does the job. When trying to match if there is msg.payload.length I got this ""TypeError: Cannot read property 'length' of null"".

if (msg.payload=== null) {  
msg.payload = 0;  
}  
return msg;
