# How to use flow variabile without modifing it

**URL:** <https://discourse.nodered.org/t/how-to-use-flow-variabile-without-modifing-it/52937>\
**Category:** General\
**Created:** [29 October 2021 11:43 UTC](https://discourse.nodered.org/t/how-to-use-flow-variabile-without-modifing-it/52937 "2021-10-29T11:43:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giu-seppe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/giu-seppe/32/40489_2.png) [@Giu-seppe](https://discourse.nodered.org/u/Giu-seppe)\
**Post date:** [29 October 2021 11:43 UTC](https://discourse.nodered.org/t/how-to-use-flow-variabile-without-modifing-it/52937/1 "2021-10-29T11:43:03Z")

</div>

Hi all, can anyone explain to me why the flow's variable created with the first inject node is modified in the other two without using the flow.set() method?

```auto
[{"id":"26a58557.18feca","type":"inject","z":"938038fa.b84288","name":"Make a flow variable","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"IdDispositivo\":8,\"SystemGroup\":\"OLCIS\",\"IP\":\"192.168.1.132\",\"ShutdownDelay\":\"0\"}","payloadType":"json","x":170,"y":100,"wires":[["9a5d3c3a.80b9f"]]},{"id":"c566479c.b8dae8","type":"function","z":"938038fa.b84288","name":"setFlow.DeviceToPayload","func":"let x = flow.get('device')||0;\nlet payload = x;\npayload.count = 0;\nreturn {payload};","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":190,"y":400,"wires":[[]]},{"id":"9a5d3c3a.80b9f","type":"change","z":"938038fa.b84288","name":"","rules":[{"t":"set","p":"device","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":180,"y":160,"wires":[[]]},{"id":"a3defa86.57a188","type":"inject","z":"938038fa.b84288","name":"take flow variable to use it in the msg without modifing flow variable","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":280,"y":220,"wires":[["72dde4c6.6c969c"]]},{"id":"72dde4c6.6c969c","type":"change","z":"938038fa.b84288","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"device","tot":"flow"},{"t":"set","p":"payload.count","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":160,"y":280,"wires":[[]]},{"id":"6560276b.544388","type":"inject","z":"938038fa.b84288","name":"Do the some but in a function","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":160,"y":340,"wires":[["c566479c.b8dae8"]]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [29 October 2021 12:02 UTC](https://discourse.nodered.org/t/how-to-use-flow-variabile-without-modifing-it/52937/2 "2021-10-29T12:02:31Z")

</div>

Because JS objects are copied and stored by reference

> **[Object references and copying](https://javascript.info/object-copy)**

Clone the message  
[https://nodered.org/docs/api/modules/v/1.3/@node-red\_util\_util.html](https://nodered.org/docs/api/modules/v/1.3/@node-red_util_util.html)

```auto
let x = flow.get('device')||0;
let payload = RED.util.cloneMessage(x);
payload.count = 0;
return {payload};

```

---

<div class="post-metadata">

**Author:** ![Giu-seppe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/giu-seppe/32/40489_2.png) [@Giu-seppe](https://discourse.nodered.org/u/Giu-seppe)\
**Post date:** [29 October 2021 12:45 UTC](https://discourse.nodered.org/t/how-to-use-flow-variabile-without-modifing-it/52937/3 "2021-10-29T12:45:18Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6f6646a6914ff83b6866572b4487d1ffb5755801.png)  
OK! I got it  
now ther's a new problem

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [29 October 2021 12:51 UTC](https://discourse.nodered.org/t/how-to-use-flow-variabile-without-modifing-it/52937/4 "2021-10-29T12:51:56Z")

</div>

My fault typo on "Red" should be "RED"

---

<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:** [12 November 2021 12:52 UTC](https://discourse.nodered.org/t/how-to-use-flow-variabile-without-modifing-it/52937/5 "2021-11-12T12:52:13Z")

</div>

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