# Context flow strange behavior/bug?

**URL:** <https://discourse.nodered.org/t/context-flow-strange-behavior-bug/77056>\
**Category:** General\
**Created:** [29 March 2023 13:29 UTC](https://discourse.nodered.org/t/context-flow-strange-behavior-bug/77056 "2023-03-29T13:29:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vilnis\_R](https://avatars.discourse-cdn.com/v4/letter/v/a183cd/32.png) [@Vilnis\_R](https://discourse.nodered.org/u/Vilnis_R)\
**Post date:** [29 March 2023 13:29 UTC](https://discourse.nodered.org/t/context-flow-strange-behavior-bug/77056/1 "2023-03-29T13:29:03Z")

</div>

I have created 5 context flows named Chart0, Chart1 .....Chart5 that stores chart values like so:

```auto
[{"series":[""],"data":[[{"x":1680087534038,"y":10},{"x":1680095175649,"y":22.1},{"x":1680095226258,"y":22.1},{"x":1680095276868,"y":22.1},{"x":1680095327498,"y":22.1},{"x":1680095378102,"y":22.1}]],"labels":[""]}]

```

Then I have incoming json:

```auto
{"0":{"T":22.1,"R":1,"P":26,"L":1},"1":{"T":22.1,"R":1,"P":26,"L":1},"2":{"T":22.6,"R":0,"P":26,"L":2},"3":{"R":1},"4":{"R":1}}

```

Then I made the function node that pushes new data into these flows using following code:

```auto
var d = new Date();
var epoch = d.getTime();
var incoming = msg.payload;

/* initial code that should do the batch process
for (var i = 0; i < Object.keys(data).length; i++) {
    if (data[i] && data[i].T) {
        var Last = flow.get("Chart"+String(i));
        var point = {"x": epoch, "y": data[i].T};
        Last[0].data[0].push(point);
        flow.set("Chart" + String(i), Last);
    }
}
*/
// short test flow to narrow the problem > that adds data to Chart1...5 deliberately too!!!
var Last = flow.get("Chart0");
var point = { "x": epoch, "y": incoming[0].T };
Last[0].data[0].push(point);
flow.set("Chart0", Last);

```

So I found out if I name the flow for instance Chart\_x then the function adds data as intended into Chart\_x. However if I make number pattern name like Chart0 or Chart\_0c then the function will fill up all similar pattern flows while I am not calling these flows in my code.

So what I am missing here?

---

<div class="post-metadata">

**Author:** ![Vilnis\_R](https://avatars.discourse-cdn.com/v4/letter/v/a183cd/32.png) [@Vilnis\_R](https://discourse.nodered.org/u/Vilnis_R)\
**Post date:** [29 March 2023 13:53 UTC](https://discourse.nodered.org/t/context-flow-strange-behavior-bug/77056/2 "2023-03-29T13:53:31Z")

</div>

Just an update. I created initially these flows using separate function. To get ChartA, ChartB...:

```auto
var flow_names = ["A", "B", "C", "D", "E"];
var newmsg = [{ "series": [""], "data": [[{ "x": 1680087534038, "y": 10 }]], "labels": [""] }]
for (var i = 0; i < 5; i++) {
    if (flow.get("Chart" + flow_names[i]) === undefined) {
        flow.set("Chart" + flow_names[i], newmsg)
    }
}

```

Result is the same. If I call ChartA, then it fills in all \> ChartA, ChartB ... but not Chart\_x or Chart\_0c, Chart\_1c's which I left from previous experimenting.

---

<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:** [29 March 2023 14:13 UTC](https://discourse.nodered.org/t/context-flow-strange-behavior-bug/77056/3 "2023-03-29T14:13:54Z")

</div>

You are falling over the javascript feature that objects and arrays are referenced, effectively, by using pointers. So when you say `flow.set("Chart" + flow_names[i], newmsg)` you are actually saving a pointer to newmsg. Then when you do the same for the next flow, it is also saving a pointer to the same array.  
Later if you change the contents of that array then, since all the context vars are pointing to the same array, you will change all of them.  
The solution is make a new copy of the array when you initialise the context var.

By the way, I suggest not using variable names like newmsg unless they are actually messages. Otherwise at some point you will think you can return that at the end of a function node, and it will fail.

---

<div class="post-metadata">

**Author:** ![Vilnis\_R](https://avatars.discourse-cdn.com/v4/letter/v/a183cd/32.png) [@Vilnis\_R](https://discourse.nodered.org/u/Vilnis_R)\
**Post date:** [29 March 2023 14:14 UTC](https://discourse.nodered.org/t/context-flow-strange-behavior-bug/77056/4 "2023-03-29T14:14:07Z")

</div>

I am starting to think whether it can be related to the fact that I am batch creating these flows initially. I noticed that these flows has their own ID & timestamp that is hidden. Is it possible, that batch creation assign all these flows with the same ID/Timestamp and this cause the problem?

---

<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:** [29 March 2023 14:14 UTC](https://discourse.nodered.org/t/context-flow-strange-behavior-bug/77056/5 "2023-03-29T14:14:41Z")

</div>

See my post that crossed with yours.

---

<div class="post-metadata">

**Author:** ![Vilnis\_R](https://avatars.discourse-cdn.com/v4/letter/v/a183cd/32.png) [@Vilnis\_R](https://discourse.nodered.org/u/Vilnis_R)\
**Post date:** [29 March 2023 14:58 UTC](https://discourse.nodered.org/t/context-flow-strange-behavior-bug/77056/6 "2023-03-29T14:58:53Z")

</div>

Yes, that was the cause for my problem. I have deleted all the previous flows and created the same name flows separately and now it works as intended! Thanks a lot!

Now I have to figure out how to effectively initiate them in case if for some reason these flows are absent.

I will bear in mind Your advice for variable names! 🙂

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [29 March 2023 15:25 UTC](https://discourse.nodered.org/t/context-flow-strange-behavior-bug/77056/7 "2023-03-29T15:25:51Z")

</div>

Just a teeny weeny small thing 😁

**Flow** - A Collection of nodes and wires in a tab  
**Context** - An operating environment/scope, we have a context for `flow`, `global` and `node`

So when you do `flow.set('SomeName')`  
You are setting a variable named `SomeName` in the flow's context, not creating a flow.

You can also create a global variable, by setting it against the global context `global.set('SomeName')`

Therefore you are not creating flows, you are creating variables stored in the flow's context

Referring to a variable as a flow might cause the space time continuum to collapse 😱

See  
[https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)

---

<div class="post-metadata">

**Author:** ![Vilnis\_R](https://avatars.discourse-cdn.com/v4/letter/v/a183cd/32.png) [@Vilnis\_R](https://discourse.nodered.org/u/Vilnis_R)\
**Post date:** [29 March 2023 15:35 UTC](https://discourse.nodered.org/t/context-flow-strange-behavior-bug/77056/8 "2023-03-29T15:35:43Z")

</div>

You are right! 🙂 I was a bit nervous and shortened flow context to flow. My head was full of variables - i.e. got too many parameters error in my head. Sorry for that.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [29 March 2023 15:38 UTC](https://discourse.nodered.org/t/context-flow-strange-behavior-bug/77056/9 "2023-03-29T15:38:35Z")

</div>

> Sorry for that.

Dont apologise, I just don't like seeing fairies falling from the sky 😂

---

<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 April 2023 15:38 UTC](https://discourse.nodered.org/t/context-flow-strange-behavior-bug/77056/10 "2023-04-12T15:38:39Z")

</div>

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