# Making a simple cloning node

**URL:** <https://discourse.nodered.org/t/making-a-simple-cloning-node/17143>\
**Category:** Share Your Nodes\
**Created:** [25 October 2019 13:51 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143 "2019-10-25T13:51:27Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [25 October 2019 13:51 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/1 "2019-10-25T13:51:28Z")

</div>

Following this discussion

> [@Changes to Context Flow Variable also changes source array it was extracted from](https://discourse.nodered.org/t/changes-to-context-flow-variable-also-changes-source-array-it-was-extracted-from/17070):
>
> I have found what I think is anomalous behavior (from what I expected) when changing an element in a context Flow Object Variable that was itself extracted as one object in an array of objects from a object in Context Flow variables. To explain further as demonstrated in this Flow. We create a array of JSON objects flow.sZones. From that we extract one object from the array into flow.sZone = flow.sZones.Zone[4]. Now I change one element in flow.sZone to a new value but it also changes the c…

I'm thinking of making a simple contrib node to let users make a copy of an object e.g msg/flow/global.xxx to msg/flow/global.yyy

(I searched for clone and copy in the palette and didn't find anything suitable)

But just posting this in case someone says - no need - you can simply use zzzz node to do it

Doing this mainly as a learning exercise for me in creating a working contrib node

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [25 October 2019 14:58 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/2 "2019-10-25T14:58:08Z")

</div>

Hi Simon,

You could use my **node-red-contrib-msg-resend** with number of resends equal to `1` and [force-cloning-advanced](https://github.com/bartbutenaers/node-red-contrib-msg-resend#force-cloning-advanced) activated. But I think publishing a new extra simple **node-red-contrib-msg-clone** node cannot harm anybody, and is much more clear in what it does ...  
Bart

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [25 October 2019 15:03 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/3 "2019-10-25T15:03:37Z")

</div>

> [@BartButenaers](#):
>
> You could use my **node-red-contrib-msg-resend** with number of resends equal to `1` and [force-cloning-advanced](https://github.com/bartbutenaers/node-red-contrib-msg-resend#force-cloning-advanced) activated.

That only clones the whole message and not any flow or global context properties

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [25 October 2019 15:40 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/4 "2019-10-25T15:40:28Z")

</div>

That is true. Go for it, I would say 😉

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [25 October 2019 15:45 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/5 "2019-10-25T15:45:14Z")

</div>

Isn't this doable with just a one-line `function` node?

```auto
return RED.util.cloneMessage(msg);

```

---

<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:** [25 October 2019 16:08 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/6 "2019-10-25T16:08:17Z")

</div>

> [@shrickus](#):
>
> RED.util.cloneMessage(msg);

Does that do a deep clone? In other words if msg.payload is a reference to an object in global context will it make a copy of the object itself or just make a copy of the reference?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [25 October 2019 16:15 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/7 "2019-10-25T16:15:21Z")

</div>

> [@shrickus](#):
>
> Isn't this doable with just a one-line `function` node?

Yes if you just want a copy of the msg.

This is for something that lets you choose what object you want you want to copy inc flow/global contexts - and to give a choice of where to stick the copy

[edit]And for users who want to avoid writing functions[/edit]

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [25 October 2019 16:15 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/8 "2019-10-25T16:15:37Z")

</div>

Good Question: all I know about it is what's in the Function Node section of the User guide:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/bb217d31a80d47c534f6d6db5d1f791e9568d8f5.png)

But I'm assuming Nick has worked through the details of what cloning the `msg` object should do...

---

<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:** [25 October 2019 16:19 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/9 "2019-10-25T16:19:39Z")

</div>

I _think_ that cloneMessage has a couple of tricks - doesn't it strip circular references and possibly some of the c\*\*\*p that ExpressJS adds to an incoming request. Otherwise, I think that it is a straight deep clone.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [25 October 2019 16:19 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/10 "2019-10-25T16:19:44Z")

</div>

> [@cymplecy](#):
>
> And for users who want to avoid writing functions

Heh... well the idea is useful, imo -- but sounds like it should be an option on the `change` node to me. The main sticky bit will be that the `change` node does not currently let you "replace" the entire incoming msg with a changed (or cloned) object or partial object (although it has been discussed in the past as a good thing to have).

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [25 October 2019 16:22 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/11 "2019-10-25T16:22:07Z")

</div>

> [@TotallyInformation](#):
>
> I _think_ that cloneMessage has a couple of tricks - doesn't it strip circular references and possibly some of the c\*\*\*p that ExpressJS adds to an incoming request. Otherwise, I think that it is a straight deep clone.

> <https://github.com/node-red/node-red/blob/0ff324b0db65ed783897550b49adb736eca42beb/packages/node_modules/%40node-red/util/lib/util.js#L75-L100>

in which `clone()` is indeed deep-cloning: [clone - npm](https://www.npmjs.com/package/clone)

---

<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:** [25 October 2019 16:24 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/12 "2019-10-25T16:24:36Z")

</div>

and does indeed do some sanity checking along the way.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [25 October 2019 17:04 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/13 "2019-10-25T17:04:58Z")

</div>

> [@shrickus](#):
>
> but sounds like it should be an option on the `change` node to me.

I think that would be the ultimate aim - add a copy option to SET/CHANGE/DELETE/MOVE

---

<div class="post-metadata">

**Author:** ![IanH](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ianh/32/95002_2.png) [@IanH](https://discourse.nodered.org/u/IanH)\
**Post date:** [4 November 2019 08:41 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/14 "2019-11-04T08:41:08Z")

</div>

I agree, there should be a Copy or Clone (to reuse current Node-Red JS terminology) Also the JSONata functions (flow-get) could have an optional parameter to Clone (T/F)

---

<div class="post-metadata">

**Author:** ![gauravdasgupta](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gauravdasgupta/32/23372_2.png) [@gauravdasgupta](https://discourse.nodered.org/u/gauravdasgupta)\
**Post date:** [27 July 2020 06:13 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/15 "2020-07-27T06:13:15Z")

</div>

Hi @cymplecy, just read through the complete thread. It was very interesting for me since I am trying to achieve something similar. So Just want to know if this Node available today ?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [27 July 2020 07:12 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/16 "2020-07-27T07:12:34Z")

</div>

Sorry - no - I've never got round to making it ☹

---

<div class="post-metadata">

**Author:** ![gauravdasgupta](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gauravdasgupta/32/23372_2.png) [@gauravdasgupta](https://discourse.nodered.org/u/gauravdasgupta)\
**Post date:** [27 July 2020 09:29 UTC](https://discourse.nodered.org/t/making-a-simple-cloning-node/17143/17 "2020-07-27T09:29:07Z")

</div>

No problem at all.. Thanks for the confirmation! 🙂
