# Problem with global.get in a function

**URL:** https://discourse.nodered.org/t/problem-with-global-get-in-a-function/90621
**Category:** General
**Created:** [1 September 2024 16:42 UTC](https://discourse.nodered.org/t/problem-with-global-get-in-a-function/90621 "2024-09-01T16:42:32Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![irotier](https://avatars.discourse-cdn.com/v4/letter/i/aca169/32.png) [@irotier](https://discourse.nodered.org/u/irotier)
#### Post date: [1 September 2024 16:42 UTC](https://discourse.nodered.org/t/problem-with-global-get-in-a-function/90621/1 "2024-09-01T16:42:32Z")

</div>

I tried several hours to work with a global (and persistant) variable. After many tries i realized, that everything works fine, when i'm using context, saved as memoryOnly, but it doesn't if i try to use a global context wich is saved to file...

I've followed the NodeRed docs and used this in my settings.js:

```auto
contextStorage: {
   default: "memoryOnly",
   memoryOnly: { module: 'memory' },
   file: { module: 'localfilesystem' }
}

```

If i now create a global Variable with this function:

```auto
global.set("lightsPower",msg.payload);
return msg;

```

i can read and use it like this:

```auto
var M = global.get("lightsPower");
msg.payload = M;

return msg;

```

But if i create it with a change-node and select file instead of memoryOnly as destination (?!) i can't get it, no matter what i tried.  
Do i need to point to the filesystem in anyway?

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [1 September 2024 17:16 UTC](https://discourse.nodered.org/t/problem-with-global-get-in-a-function/90621/2 "2024-09-01T17:16:59Z")

</div>

Have you re-started node-RED since editing the settings.js file? It needs a restart to read in the new setting.

What do you get with this?

```auto
If you now create a global Variable with this function:

global.set("lightsPower",msg.payload, "file");
return msg;

can you read and use it like this:

var M = global.get("lightsPower", "file");
msg.payload = M;
return msg;

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [1 September 2024 17:24 UTC](https://discourse.nodered.org/t/problem-with-global-get-in-a-function/90621/3 "2024-09-01T17:24:32Z")

</div>

> [@irotier](#):
>
> I've followed the NodeRed docs and used this in my settings.js:

It is a bit hidden, as the info you required is in the docs under Writing Functions Guide.  
[https://nodered.org/docs/user-guide/writing-functions#multiple-context-stores](https://nodered.org/docs/user-guide/writing-functions#multiple-context-stores)  
Link is just below Using Context in a Flow.  
[https://nodered.org/docs/user-guide/context#using-context-in-a-flow](https://nodered.org/docs/user-guide/context#using-context-in-a-flow)

---

<div class="post-metadata">

### Author: ![irotier](https://avatars.discourse-cdn.com/v4/letter/i/aca169/32.png) [@irotier](https://discourse.nodered.org/u/irotier)
#### Post date: [1 September 2024 17:30 UTC](https://discourse.nodered.org/t/problem-with-global-get-in-a-function/90621/4 "2024-09-01T17:30:36Z")

</div>

> [@Paul-Reed](#):
>
> ```auto
> var M = global.get("lightsPower", "file");
> 
> ```

This solved it - the section "file" was missing ... thanks a lot!

---

<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: [15 September 2024 17:31 UTC](https://discourse.nodered.org/t/problem-with-global-get-in-a-function/90621/5 "2024-09-15T17:31:03Z")

</div>

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