# Error storing data in global context

**URL:** https://discourse.nodered.org/t/error-storing-data-in-global-context/13444
**Category:** General
**Created:** [18 July 2019 13:56 UTC](https://discourse.nodered.org/t/error-storing-data-in-global-context/13444 "2019-07-18T13:56:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Hubertus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hubertus/32/10077_2.png) [@Hubertus](https://discourse.nodered.org/u/Hubertus)
#### Post date: [18 July 2019 13:56 UTC](https://discourse.nodered.org/t/error-storing-data-in-global-context/13444/1 "2019-07-18T13:56:31Z")

</div>

Hi all,

i have define a global file store to store some values permanent - wen NR runs i find the json file with the pre-defined values in ./context/global:

```auto
cat global.json
{
    "Bad": {
        "Akku": {
            "Min": 20,
            "Max": 70,
            "Load": false
        }
    },
    "Buero": {
        "Akku": {
            "Min": 20,
            "Max": 70,
            "Load": false
        }
    },
    "Schlafzimmer": {
        "Akku": {
            "Min": 20,
            "Max": 70,
            "Load": false
        }
    }

```

if i store one value e.g. "Bad.Akku.Min" in a function like this:

```auto
var Bad=global.get('Bad') || {};
Bad.Akku.Min=10;
global.set('Bad',Bad);

```

30 sec later the cat global.json:

```auto
cat global.json
{
    "Bad": {
        "Akku": {
            "Min": 10,
            "Max": 70,
            "Load": false
        }
    },
    "Buero": {
        "Akku": {
            "Min": 10,
            "Max": 70,
            "Load": false
        }
    },
    "Schlafzimmer": {
        "Akku": {
            "Min": 10,
            "Max": 70,
            "Load": false
        }
    }

```

ALL "Min" values are changed to "10" - not only the "Bad.Akku.Min"  
What's going wrong there?

Thanks  
Hubertus

---

<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: [18 July 2019 17:13 UTC](https://discourse.nodered.org/t/error-storing-data-in-global-context/13444/2 "2019-07-18T17:13:35Z")

</div>

How did you create object bad in the first place? Looks like it's a reference to the other two objects.

---

<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: [18 July 2019 17:17 UTC](https://discourse.nodered.org/t/error-storing-data-in-global-context/13444/3 "2019-07-18T17:17:55Z")

</div>

To prove this theory...

```auto
let Bad = {
        "Akku": {
            "Min": 10,
            "Max": 70,
            "Load": false
        }
    }
 let Buero = {
        "Akku": {
            "Min": 10,
            "Max": 70,
            "Load": false
        }
    }
 let Schlafzimmer = {
        "Akku": {
            "Min": 10,
            "Max": 70,
            "Load": false
        }
    }
global.set ('Bad', Bad)
global.set ('Schlafzimmer', Schlafzimmer)
global.set ('Buero', Buero)

```

This will ensure objects are individual and not just references to another.

If you do this then repeat your test, it should be fine.

---

<div class="post-metadata">

### Author: ![Hubertus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hubertus/32/10077_2.png) [@Hubertus](https://discourse.nodered.org/u/Hubertus)
#### Post date: [19 July 2019 09:01 UTC](https://discourse.nodered.org/t/error-storing-data-in-global-context/13444/4 "2019-07-19T09:01:24Z")

</div>

Thanks to Steve-Mcl - that works fine!

---

<div class="post-metadata">

### Author: ![tymarbut](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@tymarbut](https://discourse.nodered.org/u/tymarbut)
#### Post date: [26 August 2020 17:52 UTC](https://discourse.nodered.org/t/error-storing-data-in-global-context/13444/5 "2020-08-26T17:52:27Z")

</div>

I wanted to update this topic because this same issue has been absolutely plaguing me, and I found a convenient solution that's more practical for larger or iteratively-built objects or groups of objects.

For example, I have several running meters for different time periods of electricity production/consumption, and they're all objects and all have identical fields. For example, "today" has "Bought", "Sold", "Consumed", "Solar", "Inverted", etc. Other time periods contain identical fields - "thisBill" and "thisYear" and "lifetime" have the same keys inside them, which are of course supposed to store different values.

There are 10 of these time periods and each one has about 16 different metrics that it tracks, so when first building the object so it can be referenced later, rather than inelegantly setting up the object as ~160 different key:value pairs, I made an object called "each" which contains the (blank) contents of each one (e.g. "Bought":0, "Sold":0, etc.), and then added that object to each of a bunch of other objects called "today", "thisBill", "thisYear', etc., then put all that in a global context object called "counters".

Sure enough, if you write to one of them in the global context, you write to all of them. So, an easy solution that works for me:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/7/b796ff17065352fe0ce7b20d278d1e74b12c481e.png)

In short: Convert your new combined object to a string, then back to an object before storing it in the global context. 🤦‍♂️

This is kind of a dumb thing to have to do, but if you do this when you initially set up one of these objects with identical fields, it removes all the "reference" problems and creates genuinely independent objects. I can't tell you all the trouble I've gone to trying to eliminate this problem, so I hope this helps somebody out there.
