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
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
jbudd
1 October 2022 15:27
2
E1cid
1 October 2022 15:29
3
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?
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.
E1cid
1 October 2022 15:35
6
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.
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.
E1cid
1 October 2022 15:39
9
|| 0
is fine but not any other number, unless there will never be a 0
.
jbudd
1 October 2022 15:40
10
E1cid:
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
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 ?
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
E1cid
1 October 2022 15:56
14
Any variable, not just context. This is the Javascript logic
The logical OR (||) (logical disjunction) operator for a set of operands
is true if and only if one or more of its operands is true. It is typically used with
boolean (logical) values. When it is, it returns a Boolean value. However,
the ||...
edterbak:
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
@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 )
And again E1cid! thank you very much in sharing your knowledge
system
Closed
15 October 2022 16:02
16
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.