# Context updated automatically?

**URL:** <https://discourse.nodered.org/t/context-updated-automatically/75451>\
**Category:** General\
**Created:** [20 February 2023 09:59 UTC](https://discourse.nodered.org/t/context-updated-automatically/75451 "2023-02-20T09:59:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [20 February 2023 09:59 UTC](https://discourse.nodered.org/t/context-updated-automatically/75451/1 "2023-02-20T09:59:06Z")

</div>

Hi All,

I have a function node, in which I read a flow context variable, and based on some conditions, I update them. Not in all circumstances. But there is a case where the code runs through a condition which does not have a `flow.set` statement, but it looks like the context is still getting updated.  
I could be overviewing something obvious but I could not figure out what.  
My function node in high level is like this

```auto
let data = flow.get("data");

if (<condition A>) {
  data.state = "A";
  flow.set("data",data);
  node.status({...});
  return msg;
}
if (<condition B>) {
  data.state = "B";
  node.status({...});
}

```

My issue is that I am looking at the case where condition B is true (condition A is not), and in this if statement, there is no flow.set(), but still I see the change in the flow variable in the right hand side of the screen.

This should not happen right? `data` is a copy of what is stored in the context and it is not a reference to the context instance?

Thanks,  
Csongor

---

<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:** [20 February 2023 10:01 UTC](https://discourse.nodered.org/t/context-updated-automatically/75451/2 "2023-02-20T10:01:14Z")

</div>

> [@nygma2004](#):
>
> `data` is a copy of what is stored in the context and it is not a reference to the context instance?

No. `flow.get` will return the object itself. Assuming you are using an in-memory context store, then any modification will be reflected in context.

---

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [20 February 2023 10:09 UTC](https://discourse.nodered.org/t/context-updated-automatically/75451/3 "2023-02-20T10:09:34Z")

</div>

> [@knolleary](#):
>
> No. `flow.get` will return the object itself. Assuming you are using an in-memory context store, then any modification will be reflected in context.

oh, I had no idea. Thanks for the clarification. After all it is not a problem, I needed to be clear about it.  
I use mix of in-memory context store and also file storage. Is the behaviour based on the type of context store used?

---

<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:** [20 February 2023 10:15 UTC](https://discourse.nodered.org/t/context-updated-automatically/75451/4 "2023-02-20T10:15:18Z")

</div>

> [@nygma2004](#):
>
> I use mix of in-memory context store and also file storage. Is the behaviour based on the type of context store used

Yes (with a but)

If you `get` an object from either memory or file store & modify its properties, you will be modifying the object (subsequent `get`s will reflect that). However, without calling `set` you will not instruct the file store to "flush" changed stuff to file.

In simple terms, do a `flow.set("myObject", myObject, "filestore")` after modifying an object directly to instruct the file store there are changes to write to disk.

---

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [20 February 2023 10:27 UTC](https://discourse.nodered.org/t/context-updated-automatically/75451/5 "2023-02-20T10:27:05Z")

</div>

Thanks for the confirmation. From now on I start omitting `flow.set` for in-memory context variables.

---

<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:** [20 February 2023 10:55 UTC](https://discourse.nodered.org/t/context-updated-automatically/75451/6 "2023-02-20T10:55:29Z")

</div>

> [@nygma2004](#):
>
> From now on I start omitting `flow.set` for in-memory context variables

To be honest, I would recommend doing the opposite - ensure you always call `flow.set()` after modifying context. If in the future you want to use a persistent context store, you don't have to go back through all of your function nodes to add it in - risking missing one.

---

<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:** [6 March 2023 10:56 UTC](https://discourse.nodered.org/t/context-updated-automatically/75451/7 "2023-03-06T10:56:01Z")

</div>

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