Global.get('')||0 inconsistent resutls (Function node)

Hello everyone,

Im using Function node a lot.

But I am seeing irratic behaviour of node red. Atleast thats how I percieve it. Might be due to a lack of understading :slight_smile:

var nr_corr = global.get('NightReduction_current_value',"file");
msg.payload = nr_corr;
return msg;

Debug node after and inject node in front shows this produces value 0. As expected. It is also the stored value.

If I want to have a default value of 10 when the variable is undefined, I was thinking that adding "||10 " would be enough. like below

var nr_corr = global.get('NightReduction_current_value',"file")||10;
msg.payload = nr_corr;
return msg;

I expect it to produce 0 now. Because the value is known as shown in the first example.
But the debug node shows value 10 instead.

Is there something I am missing?
This behaviour is sometimes there, sometimes not.

Im currently using NodeRed version 3.0.2

many thanks in advance :smile:

Maybe you need to use persistent context?

https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115

The number 0 is considered false, so if you you fetch 0 the or would kick in, as it is considered false.. Therefore it defaults to 10.
You will need to use if else conditional to set a default. If undefined

So you are saying that every variable I have declared in global.set ("file") that might be set to the value 0 at some time, will always be showing the ||10 >> value 10 as result ??

whuuuuht? :smiley:

I am using persistent storage. But I use global variables. I work with multiple tabs. big flows. Thats why I use global instad of flow or context.

Yes, That's what i am saying. ( would have stopped at Yes but min 10 characters)

1 Like

You will need to use if else conditional to set a default. If undefined

OK. :face_with_spiral_eyes:
I have just done this ||0 at multiple places, becasue I thought it was a better and shorter way of testing if the variable exists. hahahaha..

Thank you all for the quick reply!! Much appreciated. :slight_smile:

|| 0 is fine but not any other number, unless there will never be a 0.

Hmm. Is that one of those non-positive undocumented features?
It's a bit of a landmine when a variable might validly acquire a value of 0.

So we should never use context.get('variable') || default_number ?

see above post

1 Like
var ___test = global.get('___testvalue0')||10;
msg.payload = ___test;
return msg;

Result = 10

var ___test = global.get('___testvalue1')||10;
msg.payload = ___test;
return msg;

Result = 1

E1cid is 100% correct. Thank you for this clarity.

@E1cid So any stored value in context/flow/global is subjected to this logic right?

|| is never a solution to check if the varible exists/undefined.

Just as a sneaky note:

The only case you can use || to do this, is when you have ||0
Hahaha..
If the value is 0, it will be set to 0
if the value is undefined, it will be set to 0 as well :smiley:

Any variable, not just context. This is the Javascript logic

@edterbak see post above

After reading the links you gave, I think I am right. that I CAN only use the "||0".

Can you confirm that?

In general, I store global varibles. numbers a lot. So in that case 0 would still produce 0 right?
(Difficult matter this is :slight_smile: )

And again E1cid! thank you very much in sharing your knowledge

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