# Multiple output Function nodes with multiple messages

**URL:** https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536
**Category:** General
**Created:** [25 May 2019 13:37 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536 "2019-05-25T13:37:18Z")
**Posts on this page:** 19
**Page:** 1

<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: [25 May 2019 13:37 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/1 "2019-05-25T13:37:18Z")

</div>

(And the fool continues to bash his head against the wall)

I've read this link (from search) [Writing functions](https://nodered.org/docs/writing-functions.html)  
and it is interesting.

I can sort of make it work, but when I try to apply it to the **real** flow, it doesn't.

This is an extract of the node:  
Yeah, messy comments. I'm working on that.

```auto
        node.warn("TWO");
        // It is now given the machine is offline. Clean up stuff.
        msg1 = {payload: '<font color = "red" i class="fa fa-bullseye fa-2x"></i>', host: host, topic:device + "/Off-line",device_ID:device, background:"brown"};
        msg2 = {topic: device, device_ID: device, state: 4, background:"brown" };
        
        node.warn("Sending only msg2 " + msg2); // This doesn't give me anything useful to check.
        //mode.warn(msg2); // this gives errors.
        
        return [[],msg2];

```

I want to send `msg2` out of the second port/output of the node.

The last line works on another **simple** flow with 5 nodes. But it doesn't seem to want to play the game here.

Where's the elephant?

(NEW)

If I change the line so it is:

```auto
msg2 = {payload: "Test"};

```

and leave the rest as is, it works, as in I get "Test" coming out of the second output.

I am missing something.

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [25 May 2019 13:51 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/2 "2019-05-25T13:51:23Z")

</div>

Testing your function I got this error displayed in the debug node:

> "ReferenceError: host is not defined (line 3, col 92)"

The function execution stops when hit this error. When you fix it you will see that all works well.

---

<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: [25 May 2019 13:54 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/3 "2019-05-25T13:54:03Z")

</div>

Well, given that all the names are defined in mine........

I even went as far as this:

```auto
        node.warn("Device " + device);
        node.warn("Device_ID " + device);
        msg2 = {topic: device, device_ID: device, state: 4, background:"brown" };
        node.warn("Sending only msg2 " + msg2);
        return [[],msg2];

```

I see this:

```auto
"Device PortaPi"
5/25/2019, 11:52:55 PMnode: Super Nodefunction : (warn)
"Device_ID PortaPi"
5/25/2019, 11:52:55 PMnode: Super Nodefunction : (warn)
"Sending only msg2 [object Object]"
5/25/2019, 11:52:55 PMnode: Button ColourPortaPi : msg.payload : undefined
undefined

```

So I am really missing something about the message "type" (format?)

---

<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: [25 May 2019 13:59 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/4 "2019-05-25T13:59:05Z")

</div>

Why do you return [,msg2]?  
Why not return [null,msg2]?

I suspect sending an empty array in the first element may route the msg2 to the first output.

---

<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: [25 May 2019 14:03 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/5 "2019-05-25T14:03:43Z")

</div>

I think that gives the same result to me.

This is the **BASE LINE** flow:

```auto
[{"id":"67c45087.8a6f3","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"a","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":3350,"wires":[["a92e0a0c.13e4"]]},{"id":"f71e3acc.9df94","type":"inject","z":"15af81c5.64999e","name":"","topic":"","payload":"b","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":3400,"wires":[["a92e0a0c.13e4"]]},{"id":"a92e0a0c.13e4","type":"function","z":"15af81c5.64999e","name":"","func":"var msg1 = {payload: \"This is the first message\"};\nvar msg2 = {payload: \"This is message two\", colour: \"Blue\"};\nif (msg.payload == 'a')\n{\n return [msg1, msg2];\n}\nelse\nreturn [[],msg2];\n","outputs":2,"noerr":0,"x":400,"y":3380,"wires":[["a271892b.ce759"],["69addc20.09dd2c"]]},{"id":"a271892b.ce759","type":"debug","z":"15af81c5.64999e","name":"Message 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":600,"y":3300,"wires":[]},{"id":"69addc20.09dd2c","type":"debug","z":"15af81c5.64999e","name":"Message 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":600,"y":3450,"wires":[]}]

```

I "cheated" and made the second message a bit more than just text.

If I press `a`, I see both messages out of their respective outputs.

Pressing `b` I see the second message out of output #2.

But...... On _the_ machine with _the_ flow, it just spits the dummy at me.

I just tried what you said about the `return [null,msg2]`.

Same result. It works, on the **test** flow.  
Not on the one I want it to work on.

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [25 May 2019 14:29 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/6 "2019-05-25T14:29:31Z")

</div>

So, your test flow shows that you fully understand how to send multile messages. You created a fully functional test flow (that would be nicer if you were using the `null` statement instead of an empty space).  
If I got it right you came to the conclusion that your main flow is bugged ?

---

<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: [25 May 2019 14:52 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/7 "2019-05-25T14:52:02Z")

</div>

I'm not in a position to test your flow but it would seem the problem is before the return. My guess is you have a bug somewhere that ends up with msg2.payload not being populated or being assigned an undefined value. You should look around the logic that populates msg2.payload. Double check all paths are covered.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [25 May 2019 18:19 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/8 "2019-05-25T18:19:22Z")

</div>

> [@Trying\_to\_learn](#):
>
> Same result. It works, on the **test** flow.  
> Not on the one I want it to work on.

When you say it doesn't work, what do you mean? What do you see when you put debug nodes on the outputs?

---

<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: [25 May 2019 23:28 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/9 "2019-05-25T23:28:45Z")

</div>

Ok, sorry folks. It was late last night. Got some sleep and am back at it this morning.

The flow is complicated - as they usually are.

I know this is going to be way off track, but it may help with the bigger picture.

Most of the machines (RPIs) connected to the network send data back to the main machine.  
Ping, Temperature, CPU load.

The Ping and Temperature work together to give an ONLINE signal.  
If the machine is OFFLINE, the Ping returns a signal indicating that.

If the machine is ONLINE, that signal is re-sent every interval indicating the state as online.  
One of those outputs from the first SUB-FLOW is sent to the second SUB-FLOW and that is used to set the colours of the buttons.

All things being nice, everything is happy and the buttons reflect the correct state.

Because I got _too smart_ for myself I over optimised the code and lately I discovered that the OFF-LINE signal is only sent TWICE when the device goes off line.

Thus, if the screen is refreshed (or what ever) the button colour is lost.

Originally everything works fine and what is sent out the node is correct. (Or to the best of my knowledge.)

What I am wanting to do is keep output #2 of the first SUB-FLOW sending it's data so the second SUB-FLOW gets the right information (repeatedly) to set the button colour.

The picture:  
There are inputs to "Device status" (sub-flow) and it's second output (blue arrow) goes to the second sub-flow input. (Button colour)  
"Button colour"'s #2 output is used to set the button colour.

I have done stuff with multiple outputs from a `function` node and they work.

But this is different in that the message is not a simple `msg.payload = "something"`  
It has other parameters. And I agree I am probably stuffing myself with them.

BUT!

The line/s for the output are pretty much copy/pasted from other lines which do work.  
I've found it better to copy/paste from working lines than to retype them, as that increases the possibility of mistakes happening.

So: Lines which work and copied to another line and when it is sent errors happen.  
JSON errors.  
Making the line a simple one like I said: it works.

As is, the "it doesn't work" part means when I look at the debug window from the `output` node, I just get _undefined_ if it is the complex one. (colours, and other variables included in the message body)  
If it is the simple one, I get the text.

 ![Screenshot%20from%202019-05-26%2009-00-08](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4783769bb67d9e4793cc4b4098e9a5c5eb1e7be6.png)

I'm not sure if that is helpful or not.

I'll stop here in case it isn't and no more information from this line of thought is not needed.

---

<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: [26 May 2019 00:38 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/10 "2019-05-26T00:38:28Z")

</div>

Hi.

Ok, after a lot of study and checking things, I got it working.

The _line of interest_ was not really changed.

This is the part of the flow of interest:

```auto
        // It is now given the machine is offline. Clean up stuff.
        msg1 = {payload: '<font color = "red" i class="fa fa-bullseye fa-2x"></i>', host: host, topic:device + "/Off-line",device_ID:device, background:"brown"};
        msg2 = {topic: device, device_ID: device, state: 4, background:"brown" };
        return [msg1, msg2];

```

Ok, I was doing more like: `return [[],msg2];` or `return [null,msg2];`  
Both (originally) yielded _undefined_. Now it works.  
**Maybe** the inclusion of msg1 helped.  
(Shrug) I don't know.

But now it is behaving itself.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [26 May 2019 14:05 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/11 "2019-05-26T14:05:28Z")

</div>

I can guarantee that if you change the `return [msg1,msg2]` to `return [null, msg2]` it will have no effect on what comes out of o/p 2. At least for the first message. It is possible of course that having nothing on o/p 1 may cause an error later on.

---

<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: [27 May 2019 12:00 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/12 "2019-05-27T12:00:25Z")

</div>

Yes, well....... I can't refute that now.

It is working.

At one stage when I was looking at the #2 output of the node in the real flow I was getting _undefined_.

But on the test flow I made it worked. Though that gave a simple text only output, rather than a message with extra bits.

Now it is working.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [27 May 2019 12:51 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/13 "2019-05-27T12:51:52Z")

</div>

> [@Trying\_to\_learn](#):
>
> At one stage when I was looking at the #2 output of the node in the real flow I was getting _undefined_ .

Do you mean it was sending a message with something in it undefined? If so then that was nothing to do with how you were sending it, it was what you were putting in it before sending it.

---

<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: [27 May 2019 22:08 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/14 "2019-05-27T22:08:30Z")

</div>

> [@Colin](#):
>
> Do you mean it was sending a message with something in it undefined?

Correct. The `debug` node simply kept saying _undefined_ when a message was supposed to be going out.

> [@Colin](#):
>
> If so then that was nothing to do with how you were sending it, it was what you were putting in it before sending it.

If you say.

The message was constructed in the node. It was basically a copy/paste from another line which did work.

But as I said: It is now academic. The node is doing what I want. Though the new dashboard would have resolved the problem I read.

But I got it working. When I get time I may look at it more and try to work out what was going on.

---

<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: [28 May 2019 06:43 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/15 "2019-05-28T06:43:46Z")

</div>

> [@Trying\_to\_learn](#):
>
> Correct. The `debug` node simply kept saying _undefined_ when a message was supposed to be going out.

If something was appearing in the debug sidebar then a message was definitely bring sent. And if it said `undefined` then the message you were sending did not have a payload property.

Sending a message without a payload is not the same as sending no message at all.

---

<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: [28 May 2019 07:41 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/16 "2019-05-28T07:41:19Z")

</div>

I again agree.

But this is/was the line:  
(Though I can not be 100% sure)  
as it was but it has since been changed.

```auto
msg2 = {topic: device, device_ID: device, state: 4, background:"brown" };

```

This is the new line:

```auto
msg2 = {topic: device, device_ID: device, state: 4, background:"brown" };

```

But looking at it, it is the same.

The only thing changed is the line after it:

(Old)  
(can't find one. The old version got wiped)

(New)

```auto
        return [msg1, msg2];

```

Some of the things I tried are/were - I think:

```auto
return [[],msg2[]];
return msg2[];
return null,msg2[];

```

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [18 November 2019 06:27 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/17 "2019-11-18T06:27:45Z")

</div>

Those are all three wrong.  
I use this regularly in my flows so can with certainty say that  
return [null, msg];  
Is the correct syntax.  
Johannes

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [18 November 2019 06:40 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/18 "2019-11-18T06:40:31Z")

</div>

@JGKK Pretty sure he knows by now. You are responding to a post from May.

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [18 November 2019 06:51 UTC](https://discourse.nodered.org/t/multiple-output-function-nodes-with-multiple-messages/11536/19 "2019-11-18T06:51:54Z")

</div>

🙈😂 thanks and have a nice day, Johannes
