# Unexpected setting of context data in Function Node

**URL:** <https://discourse.nodered.org/t/unexpected-setting-of-context-data-in-function-node/45439>\
**Category:** General\
**Created:** [10 May 2021 18:58 UTC](https://discourse.nodered.org/t/unexpected-setting-of-context-data-in-function-node/45439 "2021-05-10T18:58:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alanmasu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alanmasu/32/32206_2.png) [@alanmasu](https://discourse.nodered.org/u/alanmasu)\
**Post date:** [10 May 2021 18:58 UTC](https://discourse.nodered.org/t/unexpected-setting-of-context-data-in-function-node/45439/1 "2021-05-10T18:58:01Z")

</div>

Hi,  
I'm new here (and new is NodeRED form me) so I don't know if my question is really a problem;  
In a function node I placed this code:

```auto
var obj = flow.get("obj");
obj[0].a = "1";
return msg;

```

The problem is that the flow data change olso if in this function there isn't a `flow.set(...)` stantment

The flow variable 'obj' is created by an another function with this code:

```auto
var obj = [{a: "str1", b: 1, c: "11:00", d: "12:00"},
            {a: "str2", b: 2, c: "13:00", d: "14:00"},
            {a: "str3", b: 3, c: "15:00", d: "16:00"}]

flow.set("obj", obj);
msg.payload = obj;
return msg;

```

Anyone can explain me the reasons?

I thank those who will answer me in advance

Alan

---

<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:** [10 May 2021 19:15 UTC](https://discourse.nodered.org/t/unexpected-setting-of-context-data-in-function-node/45439/2 "2021-05-10T19:15:52Z")

</div>

> [@alanmasu](#):
>
> Anyone can explain me the reasons

Objects in javascript are passed by reference when you `get` the object from flow or global context it is exactly the same object (your variable is simply a reference to the original)

---

<div class="post-metadata">

**Author:** ![alanmasu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alanmasu/32/32206_2.png) [@alanmasu](https://discourse.nodered.org/u/alanmasu)\
**Post date:** [10 May 2021 20:07 UTC](https://discourse.nodered.org/t/unexpected-setting-of-context-data-in-function-node/45439/3 "2021-05-10T20:07:35Z")

</div>

Hi Steve,  
thank for the fast reply 😀  
Is there a solution to avoid this?

Alan

---

<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:** [10 May 2021 22:05 UTC](https://discourse.nodered.org/t/unexpected-setting-of-context-data-in-function-node/45439/4 "2021-05-10T22:05:31Z")

</div>

It depends what you are trying to achieve.

If you wish to have a new clone that doesn't affect the object, there is a helper function `RED.util.cloneMessage()` you can use to make a copy before modifying it.

e.g...

```auto
var obj = RED.util.cloneMessage(flow.get("obj") || {});

```

---

<div class="post-metadata">

**Author:** ![alanmasu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alanmasu/32/32206_2.png) [@alanmasu](https://discourse.nodered.org/u/alanmasu)\
**Post date:** [10 May 2021 23:03 UTC](https://discourse.nodered.org/t/unexpected-setting-of-context-data-in-function-node/45439/5 "2021-05-10T23:03:45Z")

</div>

Thank Steve,  
in the meantime I found (and tried) this code:

```auto
var objTemp = JSON.parse(JSON.stringify(obj));

```

is also good as is or there are some important differences?

Alan

---

<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:** [11 May 2021 18:34 UTC](https://discourse.nodered.org/t/unexpected-setting-of-context-data-in-function-node/45439/6 "2021-05-11T18:34:26Z")

</div>

> [@alanmasu](#):
>
> ```auto
> var objTemp = JSON.parse(JSON.stringify(obj));
> 
> ```
> 
> is also good as is or there are some important differences

The helper function takes care of special cases. Also, i don't think the serialisation to string is performed in the helper function so should be faster / safer.

---

<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:** [10 July 2021 18:34 UTC](https://discourse.nodered.org/t/unexpected-setting-of-context-data-in-function-node/45439/7 "2021-07-10T18:34:34Z")

</div>

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