# Multiple outputs of function node not working

**URL:** <https://discourse.nodered.org/t/multiple-outputs-of-function-node-not-working/13988>\
**Category:** General\
**Created:** [4 August 2019 09:44 UTC](https://discourse.nodered.org/t/multiple-outputs-of-function-node-not-working/13988 "2019-08-04T09:44:30Z")\
**Posts on this page:** 9\
**Page:** 2

<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:** [4 August 2019 10:43 UTC](https://discourse.nodered.org/t/multiple-outputs-of-function-node-not-working/13988/21 "2019-08-04T10:43:09Z")

</div>

> [@Steve-Mcl](#):
>
> it could be simplified to 1 line...
> 
> ```auto
> return [{payload : msg.payload == 0 ? 1 : 0}, {payload : msg.payload == 1 ? 1 : 0}, {payload : msg.payload == 2 ? 1 : 0}, {payload : msg.payload == 3 ? 1 : 0}];
> 
> ```

my 1 liner does work! 🙂

Proof of concept...

```auto
[{"id":"7b69f526.621aec","type":"inject","z":"56fa9896.d15ed8","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":80,"wires":[["a6091beb.8838a8"]]},{"id":"2868a3fb.7658ec","type":"inject","z":"56fa9896.d15ed8","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":120,"wires":[["a6091beb.8838a8"]]},{"id":"d9d7ecc3.46f0c","type":"inject","z":"56fa9896.d15ed8","name":"","topic":"","payload":"3","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":200,"wires":[["a6091beb.8838a8"]]},{"id":"94be3aca.6d5bf8","type":"inject","z":"56fa9896.d15ed8","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":160,"wires":[["a6091beb.8838a8"]]},{"id":"a6091beb.8838a8","type":"function","z":"56fa9896.d15ed8","name":"","func":"return [{payload : msg.payload == 0 ? 1 : 0}, {payload : msg.payload == 1 ? 1 : 0}, {payload : msg.payload == 2 ? 1 : 0}, {payload : msg.payload == 3 ? 1 : 0}];","outputs":4,"noerr":0,"x":350,"y":120,"wires":[["afe798d4.320b58"],["f2c45dcf.5cdad"],["25c8acdb.ebe174"],["7eadb353.c3a5dc"]]},{"id":"afe798d4.320b58","type":"debug","z":"56fa9896.d15ed8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":80,"wires":[]},{"id":"f2c45dcf.5cdad","type":"debug","z":"56fa9896.d15ed8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":120,"wires":[]},{"id":"25c8acdb.ebe174","type":"debug","z":"56fa9896.d15ed8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":160,"wires":[]},{"id":"7eadb353.c3a5dc","type":"debug","z":"56fa9896.d15ed8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":200,"wires":[]}]

```

That is if I'm guessing your requirements correctly.

What this does is send a message on every output BUT only one of the payloadds will be 1, the others will be 0

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 August 2019 10:51 UTC](https://discourse.nodered.org/t/multiple-outputs-of-function-node-not-working/13988/22 "2019-08-04T10:51:53Z")

</div>

@afelix  
Thanks for the clarification.

Is this "new" to how NR works?

I am sure I used to be able to do it the way I showed.

What I admit I missed is that how the values returned have to be "formatted".

So they have to be in a msg.payload format.

What confused me was that I could use variables rather than msg.... packets.

@Steve-Mcl  
Thanks.

It isn't I don't like your reply, but I don't know that _trick_.

So I never thought of doing it that way.

I appreciate your replies. I just took to @afelix 's way because they explained what I did and explained my mistake.

I just hope I remember it next time.

Both: Very much appreciated.

---

<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:** [4 August 2019 11:16 UTC](https://discourse.nodered.org/t/multiple-outputs-of-function-node-not-working/13988/23 "2019-08-04T11:16:47Z")

</div>

> [@Trying\_to\_learn](#):
>
> It isn't I don't like your reply, but I don't know that _trick_ .

Its all about learning & braking it down.

Several things come into play.

1. you MUST return an object (e.g. `{} `)
2. To fire multiple outputs you return an array `[a,b,c,d]`
3. my "1 liner" returns 4 objects - lets look at the first one...

```auto
{payload : msg.payload == 0 ? 1 : 0} // this makes use of the ternary operator
//In words: create a new object {} & if msg.payload == 0, set {payload:1} otherwise set {payload:0}
//this is done 4 times inside the return statement.

```

[ternary operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)

A breakdown of the ternary statement with comments...

```auto
{ //= start of a new object
  payload : //set property payload
  msg.payload == 0 ? //if the incoming msg.payload == 0
  1 //set THIS objects 'payload' property to 1
  : //otherwise
  0 //set it to 0
} //end of new object 

```

Or perhaps you might better understand the long hand code...

```auto
var msg1 = {}; //create an empty object
if(msg.payload == 1){ //if input msg.payload == 1
  msg1.payload = 1; //set new msg1.payload to 0
} else { //otherwise
  msg1.payload = 0; //set new msg1.payload to 0
}

```

You can see its the same but the much more verbose.

So basically, all my 1 liner does is 4 ternary statements in the `return []` statement

```auto
return [ternary1,ternary2,ternary3,ternary4]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 August 2019 11:24 UTC](https://discourse.nodered.org/t/multiple-outputs-of-function-node-not-working/13988/24 "2019-08-04T11:24:01Z")

</div>

Thanks.

Yeah. I admit I didn't know the structure.

I hope I can remember that structure (correct term?) for next time.

> [@Steve-Mcl](#):
>
> you MUST return an object (e.g. `{}` )

Yeah, I think that part has sunk in now.  
But just so I am really sure....

It is ie: `{ }`  
Eg, is example. ie is THAT IS.  
So that is the only way - right?  
It has to be formatted as a message body.

---

<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:** [4 August 2019 11:33 UTC](https://discourse.nodered.org/t/multiple-outputs-of-function-node-not-working/13988/25 "2019-08-04T11:33:17Z")

</div>

Yes, `{}` is a new empty object.

However, I RARELY create a new object in my flows. You lose all of the valuable (helpful) information that can help you debug later on.

e.g. I much prefer to do this...

```auto
msg.theAnswer = msg.payload * 10;
return msg; //msg contains original .payload AND .theAnswer and everything else!

```

instead of ...

```auto
var newMsg = {}
newMsg.payload = msg.payload * 10;
return newMsg;//this is a new object and has only .payload!

```

**reason...**  
In the 1st version, If I add a debug node after this function, I can see BOTH `msg.payload` AND `msg.theAnswer`. _In the 2nd version (where I return `newMsg`), it contains ONLY `.payload` - nothing else. All prior information has been discarded and lost forever._

Sometimes - at the end of a flow, my `msg` has accumulated all sorts of values BUT its a massive help to be able to see it all in one debug message & helps me determine what happened to the `msg` as it travelled through the flow.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 August 2019 11:36 UTC](https://discourse.nodered.org/t/multiple-outputs-of-function-node-not-working/13988/26 "2019-08-04T11:36:35Z")

</div>

Yes, I agree with that fully.

I have found that happening to me many times too.

Though if the message is going a long way, after about 3 or so nodes, the original `msg.payload` is sometimes wiped because I can track it through a few nodes and make sure it is ok into the next node.

Then if all there is good, it is a case of tracking the new `msg.payload`.

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [4 August 2019 11:44 UTC](https://discourse.nodered.org/t/multiple-outputs-of-function-node-not-working/13988/27 "2019-08-04T11:44:09Z")

</div>

> [@Trying\_to\_learn](#):
>
> Is this "new" to how NR works?
> 
> I am sure I used to be able to do it the way I showed.
> 
> What I admit I missed is that how the values returned have to be "formatted".
> 
> So they have to be in a msg.payload format.
> 
> What confused me was that I could use variables rather than msg.... packets.

No, it is not new. But until now with everything you posted about you could just use the `msg` object coming from the previous node. From what I’ve read of your projects this is (one of the) first time(s) you create your own messages from nothing. Previously you could edit existing objects, like Steve describes above. And while you worked with the message object, you didn’t have to do it from scratch. Take a look at the following page from the docs if you have some spare time.

[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

As for @Steve-Mcl I understand your methods too, but try to keep in mind that you’re explaining more advanced techniques (ternary operators do not directly improve readability) plus one-liners to someone with only minor JavaScript knowledge and experience, as shown from previous threads where we both responded to. It’s better to keep it simple for now and just make it work, then later improve it to more advanced techniques. The same with the object multiplication syntax, it definitely works, but, for example, what happens to the `_msgid` property on it if you do that? Start easy, and adding more advanced techniques to posts isn’t bad as long as the reason why it didn’t work in the first place is clear too 🙂 adding one liners and ternary isn’t bad, definitely not when you have to deal with JavaScript. But keep the target audience in mind and their level of understanding, if possible.

---

<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:** [4 August 2019 11:53 UTC](https://discourse.nodered.org/t/multiple-outputs-of-function-node-not-working/13988/28 "2019-08-04T11:53:16Z")

</div>

Hi @afelix, I do agree. Normally (even in my own code )I am quite verbose & create variables for almost everything.

However, in this instance, the OP was way off (several basic misunderstandings in the code) so I got lazy & just offered a "working" solution. Though to be fair, I offered a full explanation afterwards 🙂

Regarding readability, its easy to forget that things like ternary is a bit more advanced since now that I'm familiar with it, I find the compactness much easier to read than reams of if/else.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 August 2019 11:58 UTC](https://discourse.nodered.org/t/multiple-outputs-of-function-node-not-working/13988/29 "2019-08-04T11:58:04Z")

</div>

Thanks again both.

I have slacked off the past week or so.

Had a few things go _pear shaped_ on me and I am ...... not as confident as I was.  
And because I am not doing it daily, the retention is not as good as it should/could/would be.

☹  
Killed (supposedly) 2 Arduino's.  
Lost a WAP.  
About to get a $$$$$ bill for electrical re-wiring which wasn't expected.  
(Need to move a power point and the consequences of that.) So it still needs to be done.

And in the mean time, other jobs are queuing up.

Bed calling.  
Night!

[Previous page](https://discourse.nodered.org/t/multiple-outputs-of-function-node-not-working/13988.md?page=1)
