# Context scope and complex variables possible?

**URL:** https://discourse.nodered.org/t/context-scope-and-complex-variables-possible/76335
**Category:** General
**Created:** [9 March 2023 06:05 UTC](https://discourse.nodered.org/t/context-scope-and-complex-variables-possible/76335 "2023-03-09T06:05:03Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dakky](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dakky/32/77305_2.png) [@dakky](https://discourse.nodered.org/u/dakky)
#### Post date: [9 March 2023 06:05 UTC](https://discourse.nodered.org/t/context-scope-and-complex-variables-possible/76335/1 "2023-03-09T06:05:03Z")

</div>

Hello I want to store a javascript object in the flow context scope to keep a state over different runs of a flow  
Example:

```auto
{ 
  "lastState": {
    "device1": timestamp,
    "device2": timestamp,
   }
}

```

Something like this does not work (non wokring code):

```auto
var lastReset = flow.get("lastReset")
var device = msg.device
lastReset[device] = Date.now()
flow.set(lastReset)
return msg

```

`flow.set` expects a string?

Is this possible with a function node?

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [9 March 2023 06:49 UTC](https://discourse.nodered.org/t/context-scope-and-complex-variables-possible/76335/2 "2023-03-09T06:49:59Z")

</div>

> [@dakky](#):
>
> `flow.set(lastReset)`

flow.set expects two arguments  
the name (string) of the context variable and the value (your object in this case )

try

```auto
var lastReset = flow.get("lastReset")
var device = msg.device
lastReset[device] = Date.now()
flow.set("lastReset", lastReset )
return msg

```

you can read more about using Context in Function nodes [here](https://nodered.org/docs/user-guide/writing-functions#storing-data)

---

<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: [23 March 2023 06:50 UTC](https://discourse.nodered.org/t/context-scope-and-complex-variables-possible/76335/3 "2023-03-23T06:50:32Z")

</div>

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