# Stuck with how to resolve this problem (Global context testing)

**URL:** <https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323>\
**Category:** General\
**Created:** [28 September 2024 05:44 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323 "2024-09-28T05:44:58Z")\
**Posts on this page:** 20\
**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:** [28 September 2024 05:44 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/1 "2024-09-28T05:44:58Z")

</div>

Ok, so I have this bit of code:

```auto
const dividor = 4;
const x = parseInt(global.get("SCAN_TIME")/dividor);
msg.payload = "start";
msg.delay = x;

```

Which works nice if the `global` is set.

So how to I _catch_ it so if it isn't set, there is a _default_ value?

eg:

```auto
const dividor = 4;
const x = parseInt(global.get("SCAN_TIME")/dividor);
if (x == undefined)
{
  x = 2000
}
msg.payload = "start";
msg.delay = x;

```

Not the best, but to show you what I am thinking.  
I seem to be missing the trick to test the global context's condition.

Thanks in advance..

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [28 September 2024 05:53 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/2 "2024-09-28T05:53:13Z")

</div>

The usual way to apply a default value is like this

```auto
const scantime = global.get("SCAN_TIME") || 0

```

There is an alternative if the context variable might contain boolean false

```auto
const scantime = global.get("SCAN_TIME") ?? true

```

---

<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:** [28 September 2024 05:54 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/3 "2024-09-28T05:54:52Z")

</div>

And then:

```auto
if (scantime == 0)
{
   scantime = place value here
}

```

sort of thing?

!!!

# scantime can't be `const` as it will be changed if it is not set

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [28 September 2024 05:56 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/4 "2024-09-28T05:56:31Z")

</div>

Well I don't think you need that.

```auto
const scantime = global.get("SCAN_TIME") || place value here

```

---

<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:** [28 September 2024 06:02 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/5 "2024-09-28T06:02:20Z")

</div>

Y-e-a-h b-u-t....  
(not against you)

Say the value _should_ be.... `20000`  
All things being good, the code loads and all is good with the world.

And I sorry now if I digress.

If it isn't set, I am wanting to have a _default_ value that will _get me there_ but also subtly indicate to me that the REAL value isn't set.  
(This is kind of irrelevant but to put the cards on the table.)

Oh, and the SCAN\_TIME can be altered via other systems.  
So there is no FIXED value.

So if t isn't set, I make the value.... 15000

I don't know how the line would work:

```auto
cost scantime - global.get("SCAN_TIME") || 1500

```

if `global.get("SCAN_TIME")` is set to `20000` (or some other value.)

Again: Sorry. Does that make it clearer or just more confusing? 😉

Ooooh!

Just tried what you said and it seems to work.  
In an example `function` node.

I know you can't look in side my mind (the thought of that)  
But I seem to be confused with what the `||` does with things.

Could you (maybe) help me learn?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [28 September 2024 06:58 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/6 "2024-09-28T06:58:55Z")

</div>

> [@Trying\_to\_learn](#):
>
> But I seem to be confused with what the `||` does with things.  
> Could you (maybe) help me learn?

Hmm Let's see if I understand well enough to explain. Probably not...

Consider this code

```auto
let nation = "Scotland"
if (nation === "England" || nation === "Scotland" || nation === "Wales") {
    nation = "Great Britain"
}

```

`||` is the logical OR operator.  
The if statement is evaluated left to right.

1. Does nation equal "England"? No it doesn't, so on to the next test.
2. Does nation equal "Scotland"? Yes it does. At least one test has succeeded.  
So the `if` test returns `true` _without ever having to test_ "Wales"

In the global.get statement you imagine there are brackets around the right hand side  
`let scantime = (global.get("SCAN_TIME") || 100)`

It means "Evaluate the code in brackets and use the result to set scantime"  
And in brackets we have "Get the value of the global variable **OR** (if that fails) 100"

---

<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:** [28 September 2024 07:01 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/7 "2024-09-28T07:01:48Z")

</div>

Huge hugs.

I think that you actually _found_ the problem with how I parsed it.

Somehow I was seeing the `( )` around both parts - as per you second example.

Thank you very much.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 September 2024 12:19 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/8 "2024-09-28T12:19:14Z")

</div>

By the way, when using `||` to get a default value, it is almost always safer to use `??` Best to get into the habit now that everyone should be on node.js v18 or above.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [28 September 2024 13:10 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/9 "2024-09-28T13:10:15Z")

</div>

> [@TotallyInformation](#):
>
> it is almost always safer to use `??`

Why is it almost always safer to use a nullish coalescing operator @TotallyInformation ?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 September 2024 14:14 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/10 "2024-09-28T14:14:55Z")

</div>

> [@Paul-Reed](#):
>
> Why is it almost always safer to use a nullish coalescing operator

Because if there is ever any possibility that the left-hand-side of the `||` _ **might** _ be either _zero_ or _Boolean false_, it will not work as you expect. So you are in danger of getting a very hard to track-down bug.

The horridly named nullish coalescing operator `??` on the other hand only checks for `null` and `undefined` values, not for falsy values.

Hope that helps?

> **[Nullish coalescing operator (??) - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing)**
>
> The nullish coalescing (??) operator is a logical
> operator that returns its right-hand side operand when its left-hand side operand is
> null or undefined, and otherwise returns its left-hand side
> operand.

`||` was designed as a Boolean OR operator and only later discovered to be quite handy for picking up default values.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [28 September 2024 14:54 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/11 "2024-09-28T14:54:20Z")

</div>

So in summary as long as node.js is a recent version, I really should have suggested

```auto
global.set("SCAN_TIME", 0)
let scantime = global.get("SCAN_TIME") ?? 100 // with '||' this would return 100

```

or

```auto
global.set("unitsOfMeasure", "")
let units = global.get ("unitsOfMeasure") ?? "grams" // with '||' this would return "grams"

```

Because with `||` both 0 and empty string would evaluate as false and the default be wrongly applied.

---

<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:** [28 September 2024 15:02 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/12 "2024-09-28T15:02:30Z")

</div>

> [@jbudd](#):
>
> Because with `||` both 0 and empty string would evaluate as false and the default be wrongly applied.

yup. spot on. MDN explains these things well and lets you try it out in situ: [Nullish coalescing operator (??) - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing)

---

<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:** [28 September 2024 23:01 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/13 "2024-09-28T23:01:20Z")

</div>

Wow!

Thanks that is a big wake up call for me.

Bad news though:  
Just checked the 3 always on machine.

14.21.3  
14.18.1  
16.20.2

I think I have some catching up to do.

Quickly looking through my notes:  
How do I force an update of `node.js`?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [29 September 2024 00:01 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/14 "2024-09-29T00:01:58Z")

</div>

> [@Trying\_to\_learn](#):
>
> How do I force an update of `node.js`?

If you are using a Linux server based on Debian or a derivative, you can use Dave's update script.

Otherwise, depends on your platform. For Windows, the easiest is simply to re-install Node.js with the newer version unless you are using winget or similar.

For Linux, I prefer to use the semi-official install repo's.

Looks like Node.js have upped their game recently. They have a nice new page:

> **[Node.js — Download Node.js®](https://nodejs.org/en/download/package-manager/current)**
>
> Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

Remember to choose an LTS version though. The latest version isn't always the best.

Hmm, they seem to be recommending fnm which is a node.js version manager. Most of us don't like those since they cause confusion about installed packages typically. I'd stick with the installer for windows or a native package manager for Linux (there is a small link at the bottom of the page that takes you to those).

---

<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:** [29 September 2024 00:06 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/15 "2024-09-29T00:06:52Z")

</div>

RasPi Buster.

(Sorry, head down in something else just now)

I was sure I had this stuff _written down_ somewhere.

The only update script I can find is this one:

```auto
## Version 2:
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

# To also update node to the value given at the end.
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered) --node16

```

But nothing for `node.js`

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [29 September 2024 00:08 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/16 "2024-09-29T00:08:44Z")

</div>

> [@Trying\_to\_learn](#):
>
> But nothing for `node.js`

That's Dave's script, it includes both Node-RED and Node.js.

---

<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:** [29 September 2024 00:10 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/17 "2024-09-29T00:10:56Z")

</div>

Ok. Thanks.

They are running NR 3.0.1

That'll update them to 4.x and node .... 18 or 20?

I don't know _how far_ these old RP3s will go.

They are the newer versions - luckily.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [29 September 2024 00:11 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/18 "2024-09-29T00:11:54Z")

</div>

> [@Trying\_to\_learn](#):
>
> That'll update them to 4.x and node .... 18 or 20?

I think the script allows you to choose. Check the built-in help.

---

<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:** [29 September 2024 00:12 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/19 "2024-09-29T00:12:25Z")

</div>

Ok. Thanks.

Sorry for being a _stick in the mud_

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [29 September 2024 00:14 UTC](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323/20 "2024-09-29T00:14:55Z")

</div>

According to the comments at the top of the script, if you don't specify with --node18 or --node20, it will update to node.js18.

It looks like my Pies are all running node.js v20.

[Next page](https://discourse.nodered.org/t/stuck-with-how-to-resolve-this-problem-global-context-testing/91323.md?page=2)
