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!