# Function with 2 outputs... simply think that I don't understand

**URL:** <https://discourse.nodered.org/t/function-with-2-outputs-simply-think-that-i-dont-understand/35526>\
**Category:** General\
**Created:** [7 November 2020 21:32 UTC](https://discourse.nodered.org/t/function-with-2-outputs-simply-think-that-i-dont-understand/35526 "2020-11-07T21:32:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![denisjoshua](https://avatars.discourse-cdn.com/v4/letter/d/919ad9/32.png) [@denisjoshua](https://discourse.nodered.org/u/denisjoshua)\
**Post date:** [7 November 2020 21:32 UTC](https://discourse.nodered.org/t/function-with-2-outputs-simply-think-that-i-dont-understand/35526/1 "2020-11-07T21:32:06Z")

</div>

Hi there all,  
I have this simply function node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/a/fa8b2b509d060bdd84719ecb7165bb2a02ab205b.png)

With this code inside:

```auto
msg.payload ='payload';
msg.topic = 'topic'

msg2 = msg;

msg2.payload ='payload2';
msg2.topic = 'topic2';
//
return [msg, msg2];

```

Now I expect to have different output...  
But I don't understand why I take same output on all 2 debug nodes:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/b/9b1620d1759288c778e3360cebf2715368eae4a4.png)

I know it's some basic thinks... but anyone can explain me why please ?  
Thanks a lot  
Denis

---

<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:** [7 November 2020 21:58 UTC](https://discourse.nodered.org/t/function-with-2-outputs-simply-think-that-i-dont-understand/35526/2 "2020-11-07T21:58:11Z")

</div>

That is because of the way javascript handles objects. When you write `msg2 = msg` all you are doing is creating another Reference (or pointer) to the object that msg refers to. So when you change the contents of the object that msg refers to then since msg2 refers to the same object it also changes. If you want to make a new object with the same contents as msg you have to clone it, which you can do using  
`msg2 = RED.util.cloneMessage(msg)`

---

<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:** [7 November 2020 22:07 UTC](https://discourse.nodered.org/t/function-with-2-outputs-simply-think-that-i-dont-understand/35526/3 "2020-11-07T22:07:46Z")

</div>

Also make sure to use two outputs on the function node

```auto
[{"id":"4307c3e9.c7655c","type":"debug","z":"42d0712.0c3a21","name":"d1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":560,"y":60,"wires":[]},{"id":"b14cafea.886098","type":"debug","z":"42d0712.0c3a21","name":"d2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":580,"y":140,"wires":[]},{"id":"b2cc93a7.a83778","type":"inject","z":"42d0712.0c3a21","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":120,"wires":[["dbc34ba4.17d458"]]},{"id":"dbc34ba4.17d458","type":"function","z":"42d0712.0c3a21","name":"","func":"msg.payload ='payload';\nmsg.topic = 'topic'\n\nmsg2 = RED.util.cloneMessage(msg)\n\nmsg2.payload ='payload2';\nmsg2.topic = 'topic2';\n//\nreturn [msg, msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":410,"y":120,"wires":[["4307c3e9.c7655c"],["b14cafea.886098"]]}]

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [7 November 2020 22:13 UTC](https://discourse.nodered.org/t/function-with-2-outputs-simply-think-that-i-dont-understand/35526/4 "2020-11-07T22:13:52Z")

</div>

You could alternatively use `Object.assign`...

```auto
msg.payload ='payload';
msg.topic = 'topic'

msg1 = Object.assign({}, msg);
msg2 = msg;

msg2.payload ='payload2';
msg2.topic = 'topic2';
//
return [msg1, msg2];

```

```auto
[{"id":"f8bb35e4.ae0038","type":"inject","z":"c9c4eeb8.bad8e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":2280,"wires":[["82cdef83.e8b54"]]},{"id":"ad5eb769.75d0a8","type":"debug","z":"c9c4eeb8.bad8e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":570,"y":2250,"wires":[]},{"id":"82cdef83.e8b54","type":"function","z":"c9c4eeb8.bad8e","name":"","func":"msg.payload ='payload';\nmsg.topic = 'topic'\n\nmsg1 = Object.assign({}, msg);\n\nmsg2 = msg;\n\nmsg2.payload ='payload2';\nmsg2.topic = 'topic2';\n//\nreturn [msg1, msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":370,"y":2280,"wires":[["ad5eb769.75d0a8"],["2a82a40a.f9536c"]]},{"id":"2a82a40a.f9536c","type":"debug","z":"c9c4eeb8.bad8e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":574.765625,"y":2315,"wires":[]}]

```

---

<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:** [7 November 2020 22:38 UTC](https://discourse.nodered.org/t/function-with-2-outputs-simply-think-that-i-dont-understand/35526/5 "2020-11-07T22:38:58Z")

</div>

> [@Paul-Reed](#):
>
> You could alternatively use `Object.assign` ...

TBH, unless there is other stuff in the original `msg` the OP wants to carry over to `msg2` it'd be better (and faster) to simply create a new object...

```auto
msg.payload ='payload';
msg.topic = 'topic';
var msg2 = {
  payload: "payload2",
  topic: "topic2"
}
return [msg, msg2];

```

BUT if the OP did want to clone the original, it is recommended to use `RED.util.cloneMessage()` (for reasons I forget right now)

```auto
msg.payload = 'payload';
msg.topic = 'topic';
var msg2 = RED.util.cloneMessage(msg);
msg2.payload = "payload2";
msg2.topic = "topic2";
return [msg, msg2];

```

---

<div class="post-metadata">

**Author:** ![denisjoshua](https://avatars.discourse-cdn.com/v4/letter/d/919ad9/32.png) [@denisjoshua](https://discourse.nodered.org/u/denisjoshua)\
**Post date:** [8 November 2020 07:23 UTC](https://discourse.nodered.org/t/function-with-2-outputs-simply-think-that-i-dont-understand/35526/6 "2020-11-08T07:23:33Z")

</div>

A lot of answers... thanks all so much.  
I arrive from Cobol, so there if I MOVE an object to another, there is physically a move of all bytes  
to the other object (01 level).

Anyway... I just make a test with the normal vars "move" ... and in this case it's a normal move,  
and not only a virtual pointer to the same var.

Normally what I need is to make more outputs with same object msg,  
but with changed msg.ip in order to make more "cast node" for Google Home  
to work with the same alarm message in the same time.

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

So I solved in this mode:

```auto
msg_garage = RED.util.cloneMessage(msg); // funziona solo clonando l'oggetto 
msg_cucina = RED.util.cloneMessage(msg); // altrimenti fa rif. allo stesso
msg_altro = RED.util.cloneMessage(msg); 
//
msg_cucina.ip = '192.168.1.212'; //cucina
msg_garage.ip = '192.168.1.161'; //garage
msg_altro.ip = '192.168.1.184'; //altro
//
return [msg_garage, msg_cucina, msg_altro];
//

```

The changed IP is work now... but the "cast node" give me an error...  
`"Not able to get media file via google-tts:get key failed from google"`

I'll do a search to understand why...  
I learned a new think today 🙂  
Again thanks a lot all for the help.  
Denis

---

<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:** [22 November 2020 07:23 UTC](https://discourse.nodered.org/t/function-with-2-outputs-simply-think-that-i-dont-understand/35526/7 "2020-11-22T07:23:47Z")

</div>

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