Error storing data in global context

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:

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:

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

30 sec later the cat global.json:

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

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

To prove this theory...

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.

1 Like

Thanks to Steve-Mcl - that works fine!

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:

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

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.