# Accessing and manipulating data stored in context().global from nodejs modules

**URL:** <https://discourse.nodered.org/t/accessing-and-manipulating-data-stored-in-context-global-from-nodejs-modules/51562>\
**Category:** Developing Nodes\
**Created:** [27 September 2021 09:22 UTC](https://discourse.nodered.org/t/accessing-and-manipulating-data-stored-in-context-global-from-nodejs-modules/51562 "2021-09-27T09:22:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomaszt762](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomaszt762/32/48698_2.png) [@tomaszt762](https://discourse.nodered.org/u/tomaszt762)\
**Post date:** [27 September 2021 09:22 UTC](https://discourse.nodered.org/t/accessing-and-manipulating-data-stored-in-context-global-from-nodejs-modules/51562/1 "2021-09-27T09:22:01Z")

</div>

Hi, I have a question regarding accessing and manipulating data stored in context().global from nodejs modules.

For example a node “15virtual-device-start.js” [node-red-contrib-iot-virtual-device/15virtual-device-start.js at master · ibm-watson-iot/node-red-contrib-iot-virtual-device · GitHub](https://github.com/ibm-watson-iot/node-red-contrib-iot-virtual-device/blob/master/nodes/15virtual-device-start.js), uses

```auto
var deviceManager = require('./lib/deviceManager.js');
deviceManager(RED);

```

and “deviceManager.js” [node-red-contrib-iot-virtual-device/deviceManager.js at master · ibm-watson-iot/node-red-contrib-iot-virtual-device · GitHub](https://github.com/ibm-watson-iot/node-red-contrib-iot-virtual-device/blob/master/nodes/lib/deviceManager.js), uses cache object  
`cache = {};`

**How to store this "cache" in context().global to be accessed by other nodes, instead in memory???**

Regards  
Tomasz

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [27 September 2021 11:35 UTC](https://discourse.nodered.org/t/accessing-and-manipulating-data-stored-in-context-global-from-nodejs-modules/51562/2 "2021-09-27T11:35:21Z")

</div>

Hi Tomas, welcome to the forum.

The documentation for this is here: [Node context : Node-RED (nodered.org)](https://nodered.org/docs/creating-nodes/context)

```auto
const globalContext = this.context().global

globalContext.set('storedCache', cache)

```

Just be aware however that there is no guarentee that a context store will store the data directly, some store types have to stringify the data on store and JSON.parse it on recovery. While this normally works just fine, it can occasionally catch you out because there are some things that don't translate correctly.

---

<div class="post-metadata">

**Author:** ![tomaszt762](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomaszt762/32/48698_2.png) [@tomaszt762](https://discourse.nodered.org/u/tomaszt762)\
**Post date:** [27 September 2021 12:27 UTC](https://discourse.nodered.org/t/accessing-and-manipulating-data-stored-in-context-global-from-nodejs-modules/51562/3 "2021-09-27T12:27:10Z")

</div>

ok, but where in deviceManager.js should I put:

```auto
const globalContext = this.context().global
globalContext.set('storedCache', cache)

```

if I put it in "init()" I get `TypeError: this.context is not a function (line:20)`

How can I refer to context().global from external nodejs module?  
Is there anything like RED.\*.context().global ?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [27 September 2021 12:41 UTC](https://discourse.nodered.org/t/accessing-and-manipulating-data-stored-in-context-global-from-nodejs-modules/51562/4 "2021-09-27T12:41:36Z")

</div>

I don't know that node. But I can see that `deviceManager.js` is a library module and so will not have the correct `this` context. `this` in the referenced docs refers to a runtime node instance. You would have to pass a reference to that into the library function.

---

<div class="post-metadata">

**Author:** ![tomaszt762](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomaszt762/32/48698_2.png) [@tomaszt762](https://discourse.nodered.org/u/tomaszt762)\
**Post date:** [28 September 2021 12:24 UTC](https://discourse.nodered.org/t/accessing-and-manipulating-data-stored-in-context-global-from-nodejs-modules/51562/5 "2021-09-28T12:24:37Z")

</div>

Thanks!  
and where can I find Node-RED runtime API specifications for RED.\* ???  
Is there any method to examine running instances RED.nodes visually from dashboard?

---

<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:** [28 September 2021 12:36 UTC](https://discourse.nodered.org/t/accessing-and-manipulating-data-stored-in-context-global-from-nodejs-modules/51562/6 "2021-09-28T12:36:41Z")

</div>

There is not a lot of good documentation for the internals beyond those APIs nodes are expected to use.

If you have specific questions, I'm happy to help try to point you in the right direction. But we do need to good description of what you're trying to do.

---

<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:** [27 November 2021 12:37 UTC](https://discourse.nodered.org/t/accessing-and-manipulating-data-stored-in-context-global-from-nodejs-modules/51562/7 "2021-11-27T12:37:23Z")

</div>

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