# Function as a Switch with Multiple Outputs

**URL:** <https://discourse.nodered.org/t/function-as-a-switch-with-multiple-outputs/9553>\
**Category:** General\
**Created:** [28 March 2019 16:17 UTC](https://discourse.nodered.org/t/function-as-a-switch-with-multiple-outputs/9553 "2019-03-28T16:17:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Atrophius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/atrophius/32/7140_2.png) [@Atrophius](https://discourse.nodered.org/u/Atrophius)\
**Post date:** [28 March 2019 16:17 UTC](https://discourse.nodered.org/t/function-as-a-switch-with-multiple-outputs/9553/1 "2019-03-28T16:17:58Z")

</div>

Edit: Solved. [Function as a Switch with Multiple Outputs](https://discourse.nodered.org/t/function-as-a-switch-with-multiple-outputs/9553/6?u=atrophius)

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: [https://nodered.org/docs/writing-functions#multiple-outputs](https://nodered.org/docs/writing-functions#multiple-outputs)

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?

Thanks.

---

<div class="post-metadata">

**Author:** ![Atrophius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/atrophius/32/7140_2.png) [@Atrophius](https://discourse.nodered.org/u/Atrophius)\
**Post date:** [28 March 2019 16:19 UTC](https://discourse.nodered.org/t/function-as-a-switch-with-multiple-outputs/9553/2 "2019-03-28T16:19:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [28 March 2019 16:23 UTC](https://discourse.nodered.org/t/function-as-a-switch-with-multiple-outputs/9553/3 "2019-03-28T16:23:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Atrophius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/atrophius/32/7140_2.png) [@Atrophius](https://discourse.nodered.org/u/Atrophius)\
**Post date:** [28 March 2019 16:25 UTC](https://discourse.nodered.org/t/function-as-a-switch-with-multiple-outputs/9553/4 "2019-03-28T16:25:56Z")

</div>

It's pretty simple.

```auto
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.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [28 March 2019 16:34 UTC](https://discourse.nodered.org/t/function-as-a-switch-with-multiple-outputs/9553/5 "2019-03-28T16:34:30Z")

</div>

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":[]}]`

---

<div class="post-metadata">

**Author:** ![Atrophius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/atrophius/32/7140_2.png) [@Atrophius](https://discourse.nodered.org/u/Atrophius)\
**Post date:** [28 March 2019 16:48 UTC](https://discourse.nodered.org/t/function-as-a-switch-with-multiple-outputs/9553/6 "2019-03-28T16:48:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 March 2019 17:15 UTC](https://discourse.nodered.org/t/function-as-a-switch-with-multiple-outputs/9553/7 "2019-03-28T17:15:38Z")

</div>

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

This is one of the fundamentals of node red.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [28 March 2019 18:03 UTC](https://discourse.nodered.org/t/function-as-a-switch-with-multiple-outputs/9553/8 "2019-03-28T18:03:25Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [28 March 2019 19:42 UTC](https://discourse.nodered.org/t/function-as-a-switch-with-multiple-outputs/9553/9 "2019-03-28T19:42:24Z")

</div>

> [@zenofmud](#):
>
> 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.
