Hi,
I try to improve my code daily and today I'd like to change this code:
// 1
var bc = global.get("var_bloccocar");
if (bc == 1)
{
global.set("var_bloccocar", 0)
msg1 = {labelcolorcar:"#A4A4A4"}
}
else
{
msg1 = []
}
// 2
var bw = global.get("var_bloccowet");
if (bw == 0)
{
var bw2 = global.get("var_bloccowet2")
if (bw2 == 1)
{
global.set("var_bloccowet2", 0)
msg2 = {labelcolorwet:"#A4A4A4"}
}
else
{
msg2 = []
}
}
else
{
msg2 = []
}
.
.
.
.
return [msg1, msg2, msg3, msg4, .................];
I have a lot of this function with 8/10/14 outputs and for each I have ....
else
{
msg ..... = []
}
Is there a more "elegant" solution?
I would use null instead of []
if no msg should be sent to the corresponding output. In general I am not a friend of using function nodes in Node-Red,
1 Like
E1cid
3
Without knowing what your function does i can only suggest you set all outputs to null at the start then only alter the ones that fit the conditions
const output = [null,null,null,null....etc];
// 1
let bc = global.get("var_bloccocar");
if (bc == 1)
{
global.set("var_bloccocar", 0)
output[0] = {labelcolorcar:"#A4A4A4"}
}
// 2
let bw = global.get("var_bloccowet");
let bw2 = global.get("var_bloccowet2");
if (bw == 0 && bw2 == 1)
{
global.set("var_bloccowet2", 0)
output[1] = {labelcolorwet:"#A4A4A4"}
}
.
.
.
.
return output;
1 Like
I think this is a very good solution; thank you!
You change VAR in LET, is there a reason?
E1cid
5
var is global scope and its better to use const and let which are block scope
I'm going to study; thank you again ....
system
Closed
7
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.