# Using the functionGlobalContext

**URL:** https://discourse.nodered.org/t/using-the-functionglobalcontext/36445
**Category:** General
**Created:** [24 November 2020 12:13 UTC](https://discourse.nodered.org/t/using-the-functionglobalcontext/36445 "2020-11-24T12:13:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![heei](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@heei](https://discourse.nodered.org/u/heei)
#### Post date: [24 November 2020 12:13 UTC](https://discourse.nodered.org/t/using-the-functionglobalcontext/36445/1 "2020-11-24T12:13:55Z")

</div>

Hi,

This is my first post, so please let me know if I'm doing something wrong.

I am using node-red v1.0.6 on a Raspberry Pi 4 B.

I am currently using a custom node module in my function nodes, imported by using the functionGlobalContext in the settings.js file. the js file is saved in /.node-red/custom\_classes/Classes.js.

From the settings.js file:

```auto
functionGlobalContext: {
        Classes : require('/home/pi/.node-red/custom_classes/Classes.js')

```

Here is what I'm currently doing in a function node (simplified):

```auto
const Classes = global.get('Classes');

let device1 = new Classes.Device();
let device2 = new Classes.Device();

let devices = [];

devices.push(device1);
devices.push(device2);

global.set("devices",devices,"file")

```

Then I am using the Device objects in other function nodes like this:

```auto
var devices = global.get("devices","file");

for (var device of devices) {
    for (var tag of device.tags) {
       console.log(tag.name);
    }
}

```

This is working correctly, and i can access all the methods in the Device objects from the function nodes.

The problem is that when I reboot the raspberry pi, I get "TypeError: device.tags is not iterable". I am saving the devices array to file (using contextStorage) so I can see the devices array in the global context menu bar after reboot (see picture), with all field variables, but apparently I can't access the methods.

I don't know how to work this out, so any help with this is appreciated!

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/5/4522d124e40807d33475cab430193210a09f92e7.png)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [24 November 2020 12:37 UTC](https://discourse.nodered.org/t/using-the-functionglobalcontext/36445/2 "2020-11-24T12:37:11Z")

</div>

> [@heei](#):
>
> but apparently I can't access the methods.

You cannot persist functions to file storage.

The best you can do rebuild the objects on initialisation (e.g. use an inject set to operate on start up, read your objects in, build new objects, overwrite the context variable)

TBH, you might be better off keeping the data and functionality separate and only store / retrieve the data object.

Alternatively, could pass the `global.set` or `flow.set` function to your class instance and execute it inside your class to serialise the internal data. (and same for `get()` to re-populate an object)

---

<div class="post-metadata">

### Author: ![heei](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@heei](https://discourse.nodered.org/u/heei)
#### Post date: [24 November 2020 13:25 UTC](https://discourse.nodered.org/t/using-the-functionglobalcontext/36445/3 "2020-11-24T13:25:08Z")

</div>

Thanks for the quick reply!

> [@Steve-Mcl](#):
>
> You cannot persist functions to file storage.
> 
> The best you can do rebuild the objects on initialisation (e.g. use an inject set to operate on start up, read your objects in, build new objects, overwrite the context variable)

Ok, I was thinking I maybe had to do something like this to make it work.

> [@Steve-Mcl](#):
>
> TBH, you might be better off keeping the data and functionality separate and only store / retrieve the data object.

Thanks for the suggestion, I'll try to do that.

---

<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: [8 December 2020 13:25 UTC](https://discourse.nodered.org/t/using-the-functionglobalcontext/36445/4 "2020-12-08T13:25:13Z")

</div>

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