# Flow.get function adds data to the flow context instead of just reading it

**URL:** <https://discourse.nodered.org/t/flow-get-function-adds-data-to-the-flow-context-instead-of-just-reading-it/70460>\
**Category:** General\
**Created:** [11 November 2022 16:33 UTC](https://discourse.nodered.org/t/flow-get-function-adds-data-to-the-flow-context-instead-of-just-reading-it/70460 "2022-11-11T16:33:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin12](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@Martin12](https://discourse.nodered.org/u/Martin12)\
**Post date:** [11 November 2022 16:33 UTC](https://discourse.nodered.org/t/flow-get-function-adds-data-to-the-flow-context-instead-of-just-reading-it/70460/1 "2022-11-11T16:33:44Z")

</div>

Hello everyone,

I have some strange behaviour with the flow.get function. This function changes the context data, although I actually only want to read it out. Is it intended to do so? Is there a way that the context data remains unchanged?

Heres some sample code:

```auto
[{"id":"b3653d879155d2f6","type":"change","z":"fd0dd2d5871eaf24","name":"flow.Test","rules":[{"t":"set","p":"Test.payload","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":540,"wires":[["b25113f5c23eb568"]]},{"id":"b25113f5c23eb568","type":"function","z":"fd0dd2d5871eaf24","name":"","func":"// Safe clientId (incoming requests from Front-End)\nlet clientId = false;\nif (msg.hasOwnProperty('_socketId')) clientId = msg._socketId;\n\n// Clear incoming msg-Object\nmsg = {};\n\n// get Flow-Data\nmsg = flow.get(\"Test\");\nmsg.topic = \"test/currentdata\";\n\n// Attach SocketID to msg-Object\nif (clientId) msg._socketId = clientId;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1080,"y":540,"wires":[["3b8788620f01b367"]]},{"id":"c039cb90d75d2ae4","type":"inject","z":"fd0dd2d5871eaf24","name":"Front-End Data","props":[{"p":"payload.topic","v":"test/get/currentdata","vt":"str"},{"p":"_socketId","v":"RaNdOMsOCkeTiD","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":900,"y":580,"wires":[["b25113f5c23eb568"]]},{"id":"3b8788620f01b367","type":"debug","z":"fd0dd2d5871eaf24","name":"Test-Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1250,"y":540,"wires":[]},{"id":"ac26934567734e2f","type":"inject","z":"fd0dd2d5871eaf24","name":"API-Data","props":[{"p":"payload"},{"p":"notneeded","v":"data","vt":"str"},{"p":"other","v":"data","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"actual payload","payloadType":"str","x":760,"y":540,"wires":[["b3653d879155d2f6"]]}]

```

![Screenshot 2022-11-11 173135](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8ab37bf5a92b0c833eb8aff5257cdcb7ea2ec027.jpeg)

---

<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:** [11 November 2022 16:45 UTC](https://discourse.nodered.org/t/flow-get-function-adds-data-to-the-flow-context-instead-of-just-reading-it/70460/2 "2022-11-11T16:45:00Z")

</div>

Javascript passes objects by reference.  
so  
`msg = flow.get("Test");`  
means that if you alter msg after you tell it is equal to flowget, both the context and msg now point to same reference.  
Try  
`msg = RED.util.cloneMessage(flow.get("Test"));`  
so now msg = flowget, but it is a clone pointing to a separate reference.

---

<div class="post-metadata">

**Author:** ![Martin12](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@Martin12](https://discourse.nodered.org/u/Martin12)\
**Post date:** [11 November 2022 18:40 UTC](https://discourse.nodered.org/t/flow-get-function-adds-data-to-the-flow-context-instead-of-just-reading-it/70460/3 "2022-11-11T18:40:59Z")

</div>

That explains a lot. It's a pity that JavaScript only passes the memory address of objects to the variable.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [11 November 2022 19:58 UTC](https://discourse.nodered.org/t/flow-get-function-adds-data-to-the-flow-context-instead-of-just-reading-it/70460/4 "2022-11-11T19:58:00Z")

</div>

I think this bites everybody at one point of another…Says a man who was bitten 😂

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [11 November 2022 20:17 UTC](https://discourse.nodered.org/t/flow-get-function-adds-data-to-the-flow-context-instead-of-just-reading-it/70460/5 "2022-11-11T20:17:22Z")

</div>

> [@Martin12](#):
>
> It's a pity

It's not a pity; it's a choice. In computing there are really only two lifestyles: essentially pass-by-value or pass-by-reference. Whichever you choose, achieving the other one becomes tricky.

---

<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:** [25 November 2022 20:17 UTC](https://discourse.nodered.org/t/flow-get-function-adds-data-to-the-flow-context-instead-of-just-reading-it/70460/6 "2022-11-25T20:17:38Z")

</div>

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