# Function with two outputs

**URL:** <https://discourse.nodered.org/t/function-with-two-outputs/37050>\
**Category:** General\
**Created:** [5 December 2020 14:07 UTC](https://discourse.nodered.org/t/function-with-two-outputs/37050 "2020-12-05T14:07:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Urs-Eppenberger](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/urs-eppenberger/32/28_2.png) [@Urs-Eppenberger](https://discourse.nodered.org/u/Urs-Eppenberger)\
**Post date:** [5 December 2020 14:07 UTC](https://discourse.nodered.org/t/function-with-two-outputs/37050/1 "2020-12-05T14:07:59Z")

</div>

Dear colleagues  
I'm not new to node-red, but I'm no programming experts neither.  
While working on a flow, I tried to figure out how to create more than one message in a function node. I did read the documentation about the function node. And I thought I understood. But a very small tests showed me, that I don't.  
Here is the test. I would be grateful for an explanation, why I can't duplicate an incoming message, change payload and topic, and create two different outgoing messages.

```auto
[{"id":"87c6db9f.4b9aa8","type":"inject","z":"b46b7fa0.7b04d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test1","payload":"1","payloadType":"num","x":320,"y":340,"wires":[["702af256.a9349c","e1e2c7bb.a1d0f8"]]},{"id":"702af256.a9349c","type":"function","z":"b46b7fa0.7b04d","name":"worsk as expected","func":"var msg1 = {};\nmsg1.topic = \"test2\";\nmsg1.payload = 2;\nreturn [msg, msg1];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":540,"y":340,"wires":[["615b50d6.65b8f"],["7ac6ed31.699dc4"]]},{"id":"615b50d6.65b8f","type":"debug","z":"b46b7fa0.7b04d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":320,"wires":[]},{"id":"7ac6ed31.699dc4","type":"debug","z":"b46b7fa0.7b04d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":360,"wires":[]},{"id":"e1e2c7bb.a1d0f8","type":"function","z":"b46b7fa0.7b04d","name":"works in mysterious ways","func":"var msg1 = msg;\nmsg1.topic = \"test2\";\nmsg1.payload = 2;\nreturn [msg, msg1];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":540,"y":420,"wires":[["db315194.668608"],["4b1a05c.59d27fc"]]},{"id":"db315194.668608","type":"debug","z":"b46b7fa0.7b04d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":400,"wires":[]},{"id":"4b1a05c.59d27fc","type":"debug","z":"b46b7fa0.7b04d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":440,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [5 December 2020 14:18 UTC](https://discourse.nodered.org/t/function-with-two-outputs/37050/2 "2020-12-05T14:18:39Z")

</div>

This is a case in Javascript where its called 'assigment by reference'  
when you do `var msg1 = msg;` and change the values, **msg1** is simply a pointer in memory to **msg** and you modify this way the original message.  
This i believe applies to object and arrays and you wll not see the same with premitive variables like numbers, strings

[EDIT] you can avoid it - pointing to the same memory and hence when you change the new msg in essence changing the original one too by using the spread operator ...

```auto
var msg1 = {...msg};

```

There a nice video by dcode that better explains and demonstrates this  
**[Passing by reference vs. by value](https://www.youtube.com/watch?v=SCNFVzF75x8)**

---

<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:** [5 December 2020 14:36 UTC](https://discourse.nodered.org/t/function-with-two-outputs/37050/3 "2020-12-05T14:36:17Z")

</div>

The spread operator will not do a deep clone so this will only work if your object you are cloning is only one layer deep.  
Node-red actually provides cloning functionality in the form of

```auto
var newMsg = RED.util.cloneMessage(msg))

```

which uses one of the available npm modules for cloning.  
You can also use the hackish way which is:

```auto
var newMsg = JSON.parse(JSON.stringify(msg))

```

Johannes

Edit:  
You can read more about it here

> **[3 Ways to Clone Objects in JavaScript | SamanthaMing.com](https://www.samanthaming.com/tidbits/70-3-ways-to-clone-objects/)**
>
> Because objects in JavaScript are references values, you can't simply just copy using the =. Here are 3 ways for you to clone an object...

  
Dont know if the nodered functionality isnt also based on lodash which is mentioned in this article.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [5 December 2020 14:44 UTC](https://discourse.nodered.org/t/function-with-two-outputs/37050/4 "2020-12-05T14:44:24Z")

</div>

> [@JGKK](#):
>
> `var newMsg = JSON.parse(JSON.stringify(msg))`

Watch out for that. It is very fragile. Really, you should always wrap the JSON functions in a try/catch

```auto
var newMsg = {}
try {
    newMsg = JSON.parse(JSON.stringify(msg))
catch {}

```

---

<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:** [5 December 2020 14:45 UTC](https://discourse.nodered.org/t/function-with-two-outputs/37050/5 "2020-12-05T14:45:45Z")

</div>

> [@TotallyInformation](#):
>
> Watch out for that. It is very fragile.

👍🏻 Yes i m aware, thats why i called it hackish and linked the article.

---

<div class="post-metadata">

**Author:** ![Urs-Eppenberger](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/urs-eppenberger/32/28_2.png) [@Urs-Eppenberger](https://discourse.nodered.org/u/Urs-Eppenberger)\
**Post date:** [5 December 2020 16:53 UTC](https://discourse.nodered.org/t/function-with-two-outputs/37050/6 "2020-12-05T16:53:35Z")

</div>

Thanks, I'll go with the node-red way of cloning a message.  
This by-reference stuff freaks me out, you never know what you have and some other process might change what you think you have. I'm too old for this.  
(BTW: in my coding for my ESP based sensors (Arduino IDE) I very often make Strings (upper case) out of values, just to make sure that I have my own thing in my hand and not just a lousy pointer to a place in the memory.)  
Thanks again, cloning is the way forward.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [5 December 2020 20:34 UTC](https://discourse.nodered.org/t/function-with-two-outputs/37050/7 "2020-12-05T20:34:07Z")

</div>

Well, the advantages of references vs copies is clear and mostly works in our favour. Node-RED based on constant copies of data being passed between nodes would be horrible. And mostly it "just works". Async programming can be a pain though for that odd occasion when something gets changed and flows through all the references at the "wrong time" in your nice linear logic 😒

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [3 February 2021 20:34 UTC](https://discourse.nodered.org/t/function-with-two-outputs/37050/8 "2021-02-03T20:34:08Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
