Reseting context at beginning of flow

I have a function node where I use the context to save the information of several incoming messages.

But I figured that the context lives, still after the flow restartet. Is there an elegant way of doing that?

Here is what I am trying to do:

var local= context.get('data') || [];

local.push(msg.parts.index);

msg.downloadIndex = local;
context.set('data', local);

return msg;

After the first run:
msg.downloadIndex = [0,1]
After the second run:
msg.downloadIndex = [0,1,0,1]
After the third run:
msg.downloadIndex = [0,1,0,1,0,1]
... I think you get it.
I want that msg.downloadIndex always looks like the first run, after a run.

I hope my question makes sense.

To clearify further. At the moment I am usingflow.set and flow.get and I reset it in another function node, at the beginning of the flow.

Welcome to the forum.

If I understand correctly you can change
var local= context.get('data') || [];
to
var local= [];
Then it will ignore any previous value saved.

I think I am misunderstanding something. What exactly do you mean by flow restarted?

Hi Colin,
your first suggestion will not work.

With the restart of the flow I mean, that I have an injection node, that starts the flow everyday at 10 in the morning.

Let me check to see if I've understood what you are doing:

  • You have a flow that is triggered at 10 in the morning every day
  • That flow will cause, in some way, multiple messages to arrive at that Function node and you want to build up an array of the individual msg.parts.index properties from those messages.
  • The fact you are using msg.parts.index suggests you are using a Split node, or maybe a CSV node to generate a message sequence.

Would it be true to say the first message the Function receives each morning will have msg.parts.index set to 0? If so, you could use that as the hint to the Function node to reset the array:

var local = context.get('data') || [];
if (msg.parts.index === 0) {
   local = [];
}
...

If not, then another approach would be to change your node to use Flow Context rather than the local node context - ie swap context.get/set for flow.get/set. You could then add a Change node after the Inject node that kicks things off to set flow.data to [] before the rest of flow is triggered.

Hi knolleary,

you totally hit it home.

I already switched to flow instead of context. And I also reset the flow at tghe beginning, like you proposed. I was just wondering, if there is a more elegant way to do those things.

The more elegant way may be to avoid using context completely. What you are doing seems rather odd so I wonder whether there is a better way to solve whatever the problem is. If you wanted to explore that then if you told us what the larger problem is we may be able to suggest alternatives.

Hi Colin,
i already found a solution without flow or context

Yes, but you asked if there might be a more elegant way.

Oh this was kind of an approval to what you were saying.
In the meantime I switched my Flow so that I do not need any flow or context.
thank you for your help

1 Like

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