# Reseting context at beginning of flow

**URL:** <https://discourse.nodered.org/t/reseting-context-at-beginning-of-flow/37308>\
**Category:** General\
**Created:** [10 December 2020 16:37 UTC](https://discourse.nodered.org/t/reseting-context-at-beginning-of-flow/37308 "2020-12-10T16:37:08Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Present](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@Present](https://discourse.nodered.org/u/Present)\
**Post date:** [10 December 2020 16:37 UTC](https://discourse.nodered.org/t/reseting-context-at-beginning-of-flow/37308/1 "2020-12-10T16:37:09Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Present](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@Present](https://discourse.nodered.org/u/Present)\
**Post date:** [10 December 2020 16:41 UTC](https://discourse.nodered.org/t/reseting-context-at-beginning-of-flow/37308/2 "2020-12-10T16:41:10Z")

</div>

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

---

<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:** [10 December 2020 16:52 UTC](https://discourse.nodered.org/t/reseting-context-at-beginning-of-flow/37308/3 "2020-12-10T16:52:27Z")

</div>

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.

---

<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:** [10 December 2020 16:58 UTC](https://discourse.nodered.org/t/reseting-context-at-beginning-of-flow/37308/4 "2020-12-10T16:58:08Z")

</div>

> [@Present](#):
>
> But I figured that the context lives, still after the flow restartet

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

---

<div class="post-metadata">

**Author:** ![Present](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@Present](https://discourse.nodered.org/u/Present)\
**Post date:** [11 December 2020 08:02 UTC](https://discourse.nodered.org/t/reseting-context-at-beginning-of-flow/37308/5 "2020-12-11T08:02:26Z")

</div>

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.

---

<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:** [11 December 2020 08:24 UTC](https://discourse.nodered.org/t/reseting-context-at-beginning-of-flow/37308/6 "2020-12-11T08:24:57Z")

</div>

> [@Present](#):
>
> 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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Present](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@Present](https://discourse.nodered.org/u/Present)\
**Post date:** [11 December 2020 08:27 UTC](https://discourse.nodered.org/t/reseting-context-at-beginning-of-flow/37308/7 "2020-12-11T08:27:57Z")

</div>

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.

---

<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:** [11 December 2020 09:48 UTC](https://discourse.nodered.org/t/reseting-context-at-beginning-of-flow/37308/8 "2020-12-11T09:48:47Z")

</div>

> [@Present](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Present](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@Present](https://discourse.nodered.org/u/Present)\
**Post date:** [17 December 2020 13:47 UTC](https://discourse.nodered.org/t/reseting-context-at-beginning-of-flow/37308/9 "2020-12-17T13:47:11Z")

</div>

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

---

<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:** [17 December 2020 14:25 UTC](https://discourse.nodered.org/t/reseting-context-at-beginning-of-flow/37308/10 "2020-12-17T14:25:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Present](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@Present](https://discourse.nodered.org/u/Present)\
**Post date:** [17 December 2020 14:28 UTC](https://discourse.nodered.org/t/reseting-context-at-beginning-of-flow/37308/11 "2020-12-17T14:28:43Z")

</div>

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

---

<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:** [31 December 2020 14:28 UTC](https://discourse.nodered.org/t/reseting-context-at-beginning-of-flow/37308/12 "2020-12-31T14:28:48Z")

</div>

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