Function IF and set msg

Hi, I need help:
I want to make a conditional that if the first msg is 0 the payload will go out through a specific exit, if it is 1 through another and so on, but I can't do the code.

sorry I'm a beginner

Can you paste code instead of screenshot (I can fix your code and explain why you have issues but I won't retype what you have already typed)

Clue: let has block scope meaning if you declare (let) a variable inside the if block, it won't be available outside of the if block.

if (msg.payload[0] === '1');
{let msg1 = msg.payload;
return msg1;
}
else if (msg.payload[0] === '2');
{let msg2 = msg.pyaload;
return msg2;
}else {};
return msg1, msg2;

So, first things first. When posting code on the forum, please use the code button </> or paste code between backticks

```
paste code like this
```


Onto your issue...

This is a better way...

if (msg.payload[0] === '1') {
    return [msg, null, null]; //return msg to output 1
} else if (msg.payload[0] === '2') {
    return [null, msg, null]; //return msg to output 2
} else { 
    return [null, null, msg]; //return msg to output 3
}

Hi and welcome.

Your function would be more like this,.

if (msg.payload[0] === '1'){
    return [msg,null]; //send message output 1
}
else if (msg.payload[0] === '2'){
    return [null,msg]; // send message output 2
}
return null;

thanks a lot

@Benja if you put your cursor on the red x’s you will get some information about the reason the x is there.

Something to file away for future reference,

Hi, is there a reason one should prefer a Function node to a Switch node? Is there a performance gain? Thanks.

A switch node is (marginally) more efficient than a function node, but more importantly it is easier to make simple mistakes and typos in a Function node than in a Change node, especially for those with limited js experience. You need to test any function node to make sure it works whereas you can assume that a change node will do what you tell it.
Strictly of course a Function node will do what you tell it too, but I expect you know what I mean.

Thank you for the answer, I'll continue with Switch nodes then!

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