# Unintentionally changes in a flow variable (without flow.set)

**URL:** <https://discourse.nodered.org/t/unintentionally-changes-in-a-flow-variable-without-flow-set/87439>\
**Category:** General\
**Created:** [23 April 2024 19:25 UTC](https://discourse.nodered.org/t/unintentionally-changes-in-a-flow-variable-without-flow-set/87439 "2024-04-23T19:25:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyber](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cyber](https://discourse.nodered.org/u/Cyber)\
**Post date:** [23 April 2024 19:25 UTC](https://discourse.nodered.org/t/unintentionally-changes-in-a-flow-variable-without-flow-set/87439/1 "2024-04-23T19:25:44Z")

</div>

hi,

I am assigning the content of a flow-variable to a new variable.  
it looks like, on changing in the new variable, the content of the flow variable is also changed without any flow.set in between.

I think I have no logic error, I do not get where I change the flow variable

attached 4 flows to demonstrate.

the part which will change is the active from true to false:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/c/cc617ed60f11d1755c9594620f6ea5df33e7b344.png)

```auto
[{"id":"7596cb0ce1eff1ce","type":"function","z":"df7c9eb28d26a1c2","name":"setflowvar","func":"var existingtimeplan = JSON.parse('[{\"Active\":false,\"Power\":0,\"ScheduleType\":\"CHARGE_MAX\",\"TimeTable\":{\"Start\":\"00:00\",\"End\":\"00:01\"},\"Weekdays\":{\"Mon\":true,\"Tue\":true,\"Wed\":true,\"Thu\":true,\"Fri\":true,\"Sat\":true,\"Sun\":true}},{\"Active\":false,\"Power\":0,\"ScheduleType\":\"CHARGE_MAX\",\"TimeTable\":{\"Start\":\"00:01\",\"End\":\"00:02\"},\"Weekdays\":{\"Mon\":true,\"Tue\":true,\"Wed\":true,\"Thu\":true,\"Fri\":true,\"Sat\":true,\"Sun\":true}},{\"Active\":true,\"Power\":500,\"ScheduleType\":\"CHARGE_MAX\",\"TimeTable\":{\"Start\":\"06:00\",\"End\":\"12:00\"},\"Weekdays\":{\"Mon\":true,\"Tue\":true,\"Wed\":true,\"Thu\":true,\"Fri\":true,\"Sat\":true,\"Sun\":true}},{\"Active\":true,\"Power\":2000,\"ScheduleType\":\"CHARGE_MAX\",\"TimeTable\":{\"Start\":\"12:00\",\"End\":\"17:00\"},\"Weekdays\":{\"Mon\":true,\"Tue\":true,\"Wed\":true,\"Thu\":true,\"Fri\":true,\"Sat\":true,\"Sun\":true}},{\"Active\":false,\"Power\":10000,\"ScheduleType\":\"CHARGE_MAX\",\"TimeTable\":{\"Start\":\"19:00\",\"End\":\"22:00\"},\"Weekdays\":{\"Mon\":true,\"Tue\":true,\"Wed\":true,\"Thu\":true,\"Fri\":true,\"Sat\":true,\"Sun\":true}},{\"Active\":false,\"Power\":9000,\"ScheduleType\":\"DISCHARGE_MIN\",\"TimeTable\":{\"Start\":\"17:00\",\"End\":\"23:00\"},\"Weekdays\":{\"Mon\":true,\"Tue\":true,\"Wed\":true,\"Thu\":true,\"Fri\":true,\"Sat\":true,\"Sun\":true}},{\"Active\":false,\"Power\":2000,\"ScheduleType\":\"DISCHARGE_MIN\",\"TimeTable\":{\"Start\":\"19:00\",\"End\":\"23:00\"},\"Weekdays\":{\"Mon\":true,\"Tue\":true,\"Wed\":true,\"Thu\":true,\"Fri\":true,\"Sat\":true,\"Sun\":true}}]')\n\nflow.set('existing', existingtimeplan)\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":540,"wires":[[]]},{"id":"77bcbd1029343d15","type":"inject","z":"df7c9eb28d26a1c2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":520,"y":500,"wires":[["7596cb0ce1eff1ce"]]},{"id":"64d068f710b094de","type":"function","z":"df7c9eb28d26a1c2","name":"useflowvar","func":"var mytimeplanentry = {\n 'Active': true,\n 'Power': 2000,\n 'ScheduleType': \"CHARGE_MAX\",\n 'TimeTable': {\n 'End': \"20:00\",\n 'Start': \"06:00\"\n },\n 'Weekdays': {\n 'Fri': true,\n 'Mon': true,\n 'Sat': true,\n 'Sun': true,\n 'Thu': true,\n 'Tue': true,\n 'Wed': true\n }\n};\nvar newtimeplan = []\nnewtimeplan.push(mytimeplanentry) // first add mytimeplanentry\nvar storedtimeplan = flow.get('existing')\nfor (var timeplans in storedtimeplan) {\n //Deactivate all Plans and put them behind mytimeplanentry\n storedtimeplan[timeplans]['Active'] = false;\n newtimeplan.push(storedtimeplan[timeplans])\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":540,"wires":[[]]},{"id":"7629b19851eac8c9","type":"inject","z":"df7c9eb28d26a1c2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":700,"y":500,"wires":[["64d068f710b094de"]]}]

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [23 April 2024 19:32 UTC](https://discourse.nodered.org/t/unintentionally-changes-in-a-flow-variable-without-flow-set/87439/2 "2024-04-23T19:32:42Z")

</div>

This is a consequence of the how the in-memory context works. The `context.get()` call returns the object from context - it doesn't clone the value, so it gives you exactly what is held in context. This means modifications you make are made to the object held in context, even without the `.set()` call.

If you want to avoid this modification, you need to clone the returned object yourself.

---

<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:** [23 April 2024 19:47 UTC](https://discourse.nodered.org/t/unintentionally-changes-in-a-flow-variable-without-flow-set/87439/3 "2024-04-23T19:47:00Z")

</div>

To add to that a bit, you can think of the variable containing pointer to object, rather than a copy (clone) of the object. This is the way javascript works with objects, so for example, in this code

```auto
let v1 = {x: 3, y: 4}
let v2 = v1

```

both v1 and v2 point to the same object, so if this is followed by

```auto
v1.x = 4

```

then v2.x would also now be 4. Node red provides a clone method, so you could use  
`let storedtimeplan = RED.util.cloneMessage(context.get("existing"))`  
to get a copy of the object in context.

By the way, your code is not importable as the forum has interpreted it as markdown, in order to make code readable and usable it is necessary to surround the code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

Edit: Corrected code above as suggested by @jbudd below

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [23 April 2024 20:01 UTC](https://discourse.nodered.org/t/unintentionally-changes-in-a-flow-variable-without-flow-set/87439/4 "2024-04-23T20:01:15Z")

</div>

> [@Colin](#):
>
> ```auto
> let v1 = {x: 3, y: 4}
> let v1 = v2
> 
> ```

Should that be like this?

```auto
let v1 = {x: 3, y: 4}
let v2 = v1

```

---

<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:** [23 April 2024 20:22 UTC](https://discourse.nodered.org/t/unintentionally-changes-in-a-flow-variable-without-flow-set/87439/5 "2024-04-23T20:22:21Z")

</div>

Oops yes, I will correct it. Thanks.

---

<div class="post-metadata">

**Author:** ![Cyber](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cyber](https://discourse.nodered.org/u/Cyber)\
**Post date:** [24 April 2024 04:30 UTC](https://discourse.nodered.org/t/unintentionally-changes-in-a-flow-variable-without-flow-set/87439/6 "2024-04-24T04:30:06Z")

</div>

hi,

thank you for your help. 🙂

Have corrected the sample flows to be view- and importable.

> [@Colin](#):
>
> RED.util.cloneMessage

but, there is an difference between objects and values anyhow?

```auto
[{"id":"3eacbca25217a930","type":"inject","z":"df7c9eb28d26a1c2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":580,"y":600,"wires":[["2b359338c807f133"]]},{"id":"2b359338c807f133","type":"function","z":"df7c9eb28d26a1c2","name":"function 1","func":"flow.set('myvar', 42)\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":640,"wires":[[]]},{"id":"802dc6a019b56c27","type":"inject","z":"df7c9eb28d26a1c2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":760,"y":600,"wires":[["7dc579266d5f3948"]]},{"id":"7dc579266d5f3948","type":"function","z":"df7c9eb28d26a1c2","name":"function 2","func":"var value = flow.get('myvar');\nnode.warn(value);\nvalue = 43;\nnode.warn(flow.get('myvar'))\nnode.warn(value);\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":640,"wires":[[]]}]

```

here I get the output as expected.

how can I distinguish between a "copied" and a "linked" object or value?

is e.g. the above flowvar also affected?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/32a7452e801562d86c1b2aaeb33a3fc14aa0a196.png)

more, as your samples are also with normal variables (and not only flow variables), I am now a bit concerned of how my tons of functions behave and how I can find and fix such unintentionally links instead of clones (in my head they were until now always clones 😱 )

thanks! 🙂

---

<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:** [24 April 2024 19:58 UTC](https://discourse.nodered.org/t/unintentionally-changes-in-a-flow-variable-without-flow-set/87439/7 "2024-04-24T19:58:18Z")

</div>

> [@Cyber](#):
>
> there is an difference between objects and values anyhow?

Yes, simple values, that is Numbers, Booleans, Strings, bigint and symbol are copied, Objects, Arrays, and Buffers are addressed by Reference. They are the types that have this feature. I believe the main reason for doing it is that it is normally ok and vastly improves the efficiency of the code. Occasionally it is a nuisance. This is a feature of javascript, not specifically node-red.

> [@Cyber](#):
>
> how can I distinguish between a "copied" and a "linked" object or value?

By considering the type.

> [@Cyber](#):
>
> how I can find and fix such unintentionally links instead of clones

It is very rarely an issue. The fact that you have only just seen this shows that it is a rare problem. When it is an issue, usually it is obvious that there is something wrong, though it can be tricky to work out what is going on.

---

<div class="post-metadata">

**Author:** ![Cyber](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cyber](https://discourse.nodered.org/u/Cyber)\
**Post date:** [25 April 2024 09:41 UTC](https://discourse.nodered.org/t/unintentionally-changes-in-a-flow-variable-without-flow-set/87439/8 "2024-04-25T09:41:42Z")

</div>

thank you all for your help 🙂

---

<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:** [9 May 2024 09:42 UTC](https://discourse.nodered.org/t/unintentionally-changes-in-a-flow-variable-without-flow-set/87439/9 "2024-05-09T09:42:24Z")

</div>

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