# Object is referenced instead of cloned

**URL:** <https://discourse.nodered.org/t/object-is-referenced-instead-of-cloned/81287>\
**Category:** General\
**Created:** [13 September 2023 20:24 UTC](https://discourse.nodered.org/t/object-is-referenced-instead-of-cloned/81287 "2023-09-13T20:24:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tfuchs1987](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tfuchs1987/32/44508_2.png) [@tfuchs1987](https://discourse.nodered.org/u/tfuchs1987)\
**Post date:** [13 September 2023 20:24 UTC](https://discourse.nodered.org/t/object-is-referenced-instead-of-cloned/81287/1 "2023-09-13T20:24:56Z")

</div>

Hi Forum,

I have the following code in a function node and I wonder why Node-RED behaves the way it does.

If I say "msg.debug = msg.test" and I make changes to msg.debug, msg.text will get changed the same way. Probably I am missing something here and my programming skills are too bad. Hope you guys can help me understanding this behaviour?

```auto
msg.test = {};
msg.test.t1 = "1";
msg.test.t2 = "2";
msg.debug = msg.test;
msg.debug.t1 = "11";

return msg;

```

msg.debug.t1 and msg.test.t1 will be set to "11" instead of just msg.debug.t1 🤷‍♂️  
How can I make duplicates of an object and have it unique?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [13 September 2023 20:59 UTC](https://discourse.nodered.org/t/object-is-referenced-instead-of-cloned/81287/2 "2023-09-13T20:59:39Z")

</div>

In Javascript [object are stored by reference](https://javascript.info/object-copy)  
`msg.debug = msg.test;` tell javascript to reference the same memory space, so now msg.debug and msg.test refer to the same memory.  
Use  
`msg.debug = RED.util.cloneMessage(msg.test);`

---

<div class="post-metadata">

**Author:** ![tfuchs1987](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tfuchs1987/32/44508_2.png) [@tfuchs1987](https://discourse.nodered.org/u/tfuchs1987)\
**Post date:** [13 September 2023 21:10 UTC](https://discourse.nodered.org/t/object-is-referenced-instead-of-cloned/81287/3 "2023-09-13T21:10:43Z")

</div>

thank you very much @E1cid

I also found the official explanation but for me it was really difficult to understand:  
[Cloning messages in a flow : Node-RED (nodered.org)](https://nodered.org/blog/2019/09/13/cloning-messages)

---

<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:** [27 September 2023 21:10 UTC](https://discourse.nodered.org/t/object-is-referenced-instead-of-cloned/81287/4 "2023-09-27T21:10:54Z")

</div>

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