# Function with 2 outputs

**URL:** <https://discourse.nodered.org/t/function-with-2-outputs/6576>\
**Category:** General\
**Created:** [8 January 2019 19:21 UTC](https://discourse.nodered.org/t/function-with-2-outputs/6576 "2019-01-08T19:21:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![NickB](https://avatars.discourse-cdn.com/v4/letter/n/f08c70/32.png) [@NickB](https://discourse.nodered.org/u/NickB)\
**Post date:** [8 January 2019 19:21 UTC](https://discourse.nodered.org/t/function-with-2-outputs/6576/1 "2019-01-08T19:21:10Z")

</div>

Hi,

I know it's stupid, but I have found allready different 'methods' to control 2 outputs, and for me, none of them work.

What is the idea?  
I have 1 input & 2 outputs.

If the input is 'True', I want a '1' on output 1,  
if thhe output is 'false', I want a '1' on output 2.

That's all...

I've tried this, but nothing passes the function

```
var x = msg.payload;

if (x =="TRUE"){
  var msg_o = {payload : 1}
  return [msg_o, null]}
else if (x =="FALSE"){
   var msg_o = {payload : 1}
  return [null, msg_o]}

```

I think my returns, are good, this is what I've found on the node-red functions page

thx

---

<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:** [8 January 2019 19:32 UTC](https://discourse.nodered.org/t/function-with-2-outputs/6576/2 "2019-01-08T19:32:06Z")

</div>

You should debug input value

```auto
   var x = msg.payload;
    node.warn(x)
    //... rest of your code

```

And look what you see at debug tab.  
_"TRUE" does not equal "True"_

---

<div class="post-metadata">

**Author:** ![NickB](https://avatars.discourse-cdn.com/v4/letter/n/f08c70/32.png) [@NickB](https://discourse.nodered.org/u/NickB)\
**Post date:** [8 January 2019 20:00 UTC](https://discourse.nodered.org/t/function-with-2-outputs/6576/3 "2019-01-08T20:00:42Z")

</div>

Good tip, I need lower case letters like true and false, but still no result at the end.

```
var x = msg.payload;
 
if (x =="true"){
  var msg_o = {payload : 1} 
  return [msg_o, null]}
else if (x =="false"){
  var msg_o = {payload : 1} 
  return [null, msg_o]}

```

There really is a true or false at the input

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e3ad8596af939f4bf1fdee669f9e8757645a3691.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [8 January 2019 20:04 UTC](https://discourse.nodered.org/t/function-with-2-outputs/6576/4 "2019-01-08T20:04:57Z")

</div>

Those are _not_ the string values `"true"` and `"false"` - they are boolean values `true` and `false`.

So the test you want is

```auto
if (x == true) {

```

which is equivalent to:

```auto
if (x) {

```

All told, you can simplify your function to:

```auto
var x = msg.payload;
msg.payload = 1;
if (x) {
  return [msg, null]
} else {
  return [null, msg]
}

```

Note how it reuses the `msg` passed into the function - it is generally a best practise to pass on the message you receive rather than create a new message object as there may be other message properties on it that you want to keep ahold of.

---

<div class="post-metadata">

**Author:** ![NickB](https://avatars.discourse-cdn.com/v4/letter/n/f08c70/32.png) [@NickB](https://discourse.nodered.org/u/NickB)\
**Post date:** [9 January 2019 18:11 UTC](https://discourse.nodered.org/t/function-with-2-outputs/6576/5 "2019-01-09T18:11:09Z")

</div>

Hi Knolleary,

that worked and I understand that it now passes the original message and this solution works in my application

But I don't get why my value (payload :1) does not pass, because I do exactely the same, I only have a 'fixed' value that will go to the output.

Br

Nick

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [9 January 2019 18:42 UTC](https://discourse.nodered.org/t/function-with-2-outputs/6576/6 "2019-01-09T18:42:21Z")

</div>

If you share the code you have, we can help.
