# Deep copy value documentation

**URL:** https://discourse.nodered.org/t/deep-copy-value-documentation/73653
**Category:** General
**Created:** [14 January 2023 02:33 UTC](https://discourse.nodered.org/t/deep-copy-value-documentation/73653 "2023-01-14T02:33:08Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![FlangeMonkey](https://avatars.discourse-cdn.com/v4/letter/f/e274bd/32.png) [@FlangeMonkey](https://discourse.nodered.org/u/FlangeMonkey)
#### Post date: [14 January 2023 02:33 UTC](https://discourse.nodered.org/t/deep-copy-value-documentation/73653/1 "2023-01-14T02:33:08Z")

</div>

Hi Guys,

I have an unexpected behavior where a previous change node affects the message from another change node. For example, if I "set" or copy a message with child messages to another message and then modify the original, the copy also gets modified.

This is my example, I am not expecting "settings" messages to change within the final debug.

```auto
[{"id":"e60617c667f387a8","type":"debug","z":"a58dbabd8c4e1205","name":"debug 88","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1520,"y":620,"wires":[]},{"id":"443d81a3d4efcbde","type":"inject","z":"a58dbabd8c4e1205","name":"","props":[{"p":"topic","vt":"str"},{"p":"payload.something","v":"something","vt":"str"},{"p":"payload.else","v":"else","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"testing","x":830,"y":620,"wires":[["b7a24972e6d83626","d9d462a860918f2c"]]},{"id":"b7a24972e6d83626","type":"change","z":"a58dbabd8c4e1205","name":"","rules":[{"t":"set","p":"settings","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1000,"y":620,"wires":[["17c0a3bac991bacb","4826b91edd4cd9d7"]]},{"id":"36444a0bb979df0a","type":"change","z":"a58dbabd8c4e1205","name":"","rules":[{"t":"set","p":"payload.a","pt":"msg","to":"data","tot":"str"},{"t":"set","p":"payload.b","pt":"msg","to":"settings.something","tot":"msg"},{"t":"set","p":"payload.c","pt":"msg","to":"settings.else","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1340,"y":620,"wires":[["e60617c667f387a8"]]},{"id":"d9d462a860918f2c","type":"debug","z":"a58dbabd8c4e1205","name":"debug 91","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":980,"y":680,"wires":[]},{"id":"17c0a3bac991bacb","type":"debug","z":"a58dbabd8c4e1205","name":"debug 92","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1200,"y":680,"wires":[]},{"id":"4826b91edd4cd9d7","type":"switch","z":"a58dbabd8c4e1205","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"testing","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1170,"y":620,"wires":[["36444a0bb979df0a"]]}]

```

If I enable "Deep copy value", it works as expected, leaving the "settings" messages intact and unmodified.

I am therefore looking for documentation about "deep copy value" to try and understand why, but cannot find any info?

Thanks,

---

<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: [14 January 2023 05:21 UTC](https://discourse.nodered.org/t/deep-copy-value-documentation/73653/2 "2023-01-14T05:21:41Z")

</div>

This is fortunately/unfortunately part of the javascript language, when you create a new property with a reference to another object/property it uses a "shallow copy", see [reference documentation](https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy), it also links to the deep copy documentation.

---

<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: [14 January 2023 14:45 UTC](https://discourse.nodered.org/t/deep-copy-value-documentation/73653/3 "2023-01-14T14:45:49Z")

</div>

And worth noting that the only realistic way to do a deep clone is to serialise and unserialise it. If your object cannot be serialised, you are rather stuck. Even the new [`structuredClone`](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone) function (node.js 17+ and browsers from early-mid 2022 onwards) has to serialise the object.

Also note that if you want to use `JSON.parse(JSON.stringify(object))` **ALWAYS** wrap it in a try/catch block.

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [14 January 2023 16:52 UTC](https://discourse.nodered.org/t/deep-copy-value-documentation/73653/4 "2023-01-14T16:52:33Z")

</div>

> [@TotallyInformation](#):
>
> **ALWAYS** wrap it in a try/catch block.

I would do the same for structuredClone, since it throws an error if part of the input is not serializable.

---

<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: [14 January 2023 17:07 UTC](https://discourse.nodered.org/t/deep-copy-value-documentation/73653/5 "2023-01-14T17:07:16Z")

</div>

Indeed, I should have said that.

---

<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: [15 March 2023 17:08 UTC](https://discourse.nodered.org/t/deep-copy-value-documentation/73653/6 "2023-03-15T17:08:08Z")

</div>

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