Context flow strange behavior/bug?

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


Then I have incoming json:


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

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};
        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 };
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?

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

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.

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.

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?

See my post that crossed with yours.

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! :slight_smile:

Just a teeny weeny small thing :grin:

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 :scream:


You are right! :slight_smile: 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.

1 Like

Sorry for that.

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

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