Deep copy value documentation

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.

[{"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,

1 Like

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, it also links to the deep copy documentation.

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 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.

1 Like

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

1 Like

Indeed, I should have said that.

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