# Generate retain variables using global.set/get

**URL:** <https://discourse.nodered.org/t/generate-retain-variables-using-global-set-get/19183>\
**Category:** General\
**Created:** [15 December 2019 14:01 UTC](https://discourse.nodered.org/t/generate-retain-variables-using-global-set-get/19183 "2019-12-15T14:01:39Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![easyE4](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@easyE4](https://discourse.nodered.org/u/easyE4)\
**Post date:** [15 December 2019 14:01 UTC](https://discourse.nodered.org/t/generate-retain-variables-using-global-set-get/19183/1 "2019-12-15T14:01:39Z")

</div>

Hi guys,  
two variables that I enter in a form on the dashboard, should retain their values even after restarting node-red. It's not working in my project, so I will show you what I have done and hope you can give me a solution. Node-Red is running on a raspberry.

I have change the settings.js context storage property to:

contextStorage: {  
default: {  
module:"localfilesystem"  
},  
},

I have write a function to store the values in a global array using global.set:

// after click on submit button the function get executed  
// msg.payload = contain values written by user in form  
ip\_api=[]; // global array to store ip address and api-key provided by user  
{  
ip\_api[0]=msg.payload["easyE4 IP-Adresse"];//storing the ip address in global array.  
ip\_api[1]=msg.payload["easyE4 API-Schlüssel"];//storing the API-Key in global array.  
}  
global.set("ip",ip\_api[0]); // method to set global varible/array  
global.set("api",ip\_api[1]); // method to set global varible/array  
msg.payload = ip\_api;  
return msg;

Everything is working fine in my project, but after restart of the raspberry I have to enter values again.  
What have I done wrong?

I hope, I can get a little support from the community. 😃  
Ralf

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [15 December 2019 14:16 UTC](https://discourse.nodered.org/t/generate-retain-variables-using-global-set-get/19183/2 "2019-12-15T14:16:19Z")

</div>

The form fields will be empty after a restart, but the global variables should still exist, did you check the context data ? (you probably need to refresh it)

![Screenshot 2019-12-15 at 15.15.53](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/58b480724b311bc6c84b6e9f9554e76677758056.jpeg)

---

<div class="post-metadata">

**Author:** ![easyE4](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@easyE4](https://discourse.nodered.org/u/easyE4)\
**Post date:** [15 December 2019 14:59 UTC](https://discourse.nodered.org/t/generate-retain-variables-using-global-set-get/19183/3 "2019-12-15T14:59:36Z")

</div>

Hi bakman2,  
yes, I check this.

Before restart:

 ![global1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/316908deb43680574edf957fc8c5a7b5b86cd7b3.png)  
After restart: ![global2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/cff990b1fd6d8001a73954bad55be70a27d42ca9.png)

Ralf

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [15 December 2019 15:02 UTC](https://discourse.nodered.org/t/generate-retain-variables-using-global-set-get/19183/4 "2019-12-15T15:02:00Z")

</div>

Did you click the refresh icon ?

---

<div class="post-metadata">

**Author:** ![easyE4](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@easyE4](https://discourse.nodered.org/u/easyE4)\
**Post date:** [15 December 2019 15:04 UTC](https://discourse.nodered.org/t/generate-retain-variables-using-global-set-get/19183/5 "2019-12-15T15:04:09Z")

</div>

Yes 🙂

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [15 December 2019 15:07 UTC](https://discourse.nodered.org/t/generate-retain-variables-using-global-set-get/19183/6 "2019-12-15T15:07:17Z")

</div>

That is strange, you are not overwriting/clearing the variables anywhere ?

On the commandline could you check:

```auto
cd ~/.node-red/context/global
cat global.json

node-red-restart

cat global.json

```

---

<div class="post-metadata">

**Author:** ![easyE4](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@easyE4](https://discourse.nodered.org/u/easyE4)\
**Post date:** [15 December 2019 15:15 UTC](https://discourse.nodered.org/t/generate-retain-variables-using-global-set-get/19183/7 "2019-12-15T15:15:05Z")

</div>

I will check this.  
And no, there is no overwriting, because this are the only variables I defined.

---

<div class="post-metadata">

**Author:** ![easyE4](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@easyE4](https://discourse.nodered.org/u/easyE4)\
**Post date:** [15 December 2019 15:52 UTC](https://discourse.nodered.org/t/generate-retain-variables-using-global-set-get/19183/8 "2019-12-15T15:52:17Z")

</div>

> [@bakman2](#):
>
> cd ~/.node-red/context/global

This directory will not be found 🤔

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [15 December 2019 16:05 UTC](https://discourse.nodered.org/t/generate-retain-variables-using-global-set-get/19183/9 "2019-12-15T16:05:28Z")

</div>

```auto
contextStorage: {
default: {
module:"localfilesystem"
}, <-- this comma is redundant
},

```

Dont know if it breaks context storage option but wrong syntax anyway.

---

<div class="post-metadata">

**Author:** ![easyE4](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@easyE4](https://discourse.nodered.org/u/easyE4)\
**Post date:** [15 December 2019 17:47 UTC](https://discourse.nodered.org/t/generate-retain-variables-using-global-set-get/19183/10 "2019-12-15T17:47:22Z")

</div>

I have delete the comma, but this didn't solve my problem. 😕

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 December 2019 17:54 UTC](https://discourse.nodered.org/t/generate-retain-variables-using-global-set-get/19183/11 "2019-12-15T17:54:05Z")

</div>

1. What version of Node-RED?
2. What version of Node.js
3. please copy and paste a copy of your settings.js file (three back tics ` on the line before and after the code)
4. how are you setting the valuse in the first place and/or checking their existence before doing something with them
5. after making the correction @hotNipi suggested, did you stop and restart NR?

---

<div class="post-metadata">

**Author:** ![easyE4](https://avatars.discourse-cdn.com/v4/letter/e/a5b964/32.png) [@easyE4](https://discourse.nodered.org/u/easyE4)\
**Post date:** [15 December 2019 18:27 UTC](https://discourse.nodered.org/t/generate-retain-variables-using-global-set-get/19183/12 "2019-12-15T18:27:29Z")

</div>

Hi guys,  
I have found my mistake. I eddit the wrong settings.js.  
I'm a linux beginner, and didn't nowing, that I have to look for a hide file.  
So therefore I eddit the file found in the directory "/usr/lib/node\_modules/node-red".  
Now I have found the right file in "/home/pi/.node-red" and everythings working as expected.

Thanks for the patience!  
Ralf
