Function as a Switch with Multiple Outputs


Edit: Solved. Function as a Switch with Multiple Outputs

I have an HTTP Request that's connected to a Function that I want to use as a Switch using multiple outputs. The function has to make some modifications to the msg object and then set the request method for the next request so I can't just use a Switch node. I'm trying to make this work by returning [msg, null] to target the first output and returning [null, msg] to target the second output going by what's in the documentation here:

Instead of the expected behavior of msg being sent to one output and not the other, nothing is being sent at all to either output. Are the docs wrong or is this some kind of bug with the Function node?




It's also worth mentioning that I'm currently on v0.19.5.



I'm sure the function node and the docs are OK both. But let us peek into your function. Hard to help otherwise.



It's pretty simple.

msg.payload = msg.currentRx;

if (msg.statusCode == 200) {
    // Update
    msg.method = 'PATCH';
    return [null, msg];
else {
    // Add
    return [msg, null];

I was able to replace a null with false and it suddenly started sending output (to both), which is what led me to believe there might be some kind of bug since any null was causing it to send nothing.



Working as expected for me...

[{"id":"705e2f08.2f6758","type":"inject","z":"73cf958f.f83d04","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":94.5,"y":83,"wires":[["3f05539e.5de2c4"]]},{"id":"1c911a60.40b9f6","type":"function","z":"73cf958f.f83d04","name":"","func":"msg.payload = msg.currentRx;\n\nif (msg.statusCode == 200) {\n // Update\n msg.method = 'PATCH';\n return [null, msg];\n}\nelse {\n // Add\n return [msg, null];\n}","outputs":2,"noerr":0,"x":526.5,"y":176,"wires":[["6da1b8c2.67b4e8"],["c6429e0b.1cff38"]]},{"id":"3f05539e.5de2c4","type":"change","z":"73cf958f.f83d04","name":"404","rules":[{"t":"set","p":"currentRx","pt":"msg","to":"currentRx","tot":"str"},{"t":"set","p":"statusCode","pt":"msg","to":"404","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":229.5,"y":130,"wires":[["1c911a60.40b9f6"]]},{"id":"6515c97b.16ab5","type":"inject","z":"73cf958f.f83d04","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":99,"y":196,"wires":[["657942da.83fc44"]]},{"id":"657942da.83fc44","type":"change","z":"73cf958f.f83d04","name":"200","rules":[{"t":"set","p":"currentRx","pt":"msg","to":"currentRx","tot":"str"},{"t":"set","p":"statusCode","pt":"msg","to":"200","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":234,"y":243,"wires":[["1c911a60.40b9f6"]]},{"id":"6da1b8c2.67b4e8","type":"debug","z":"73cf958f.f83d04","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":667.5,"y":133,"wires":[]},{"id":"c6429e0b.1cff38","type":"debug","z":"73cf958f.f83d04","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":661.5,"y":207,"wires":[]}]



Figured it out. Apparently the debug panel had detached or something so none of my debug messages were showing up until I refreshed the editor. It was working the whole time, I just couldn't see any debug output.

1 Like


By design, return null halts the flow (no msg sent).

This is one of the fundamentals of node red.



@Steve-Mcl Just to be clear, if the function has multiple outputs, a null only stops the flow to that output. The other output will get a msg object and that leg of the flow will continue running.



Yes of course. Only the non null output(s) will be sent.