Get everything stored in global context as object

Hi all!

I'm wondering if there's a way to quickly retrieve all key/value pairs from global context as a single object.

I know that I could change the settings.js file to store context to a file as opposed to memory only, and then use the file-in node to read that file, which does it. But, I would love to know if there was an internal way, so to speak.

Thanks!

This will give you the property (key) names of all of the global variables:

[
    {
        "id": "6642c47c.99bd3c",
        "type": "inject",
        "z": "106a5b82.ef95a4",
        "name": "Do",
        "topic": "List_All_Global_Properties",
        "payload": "",
        "payloadType": "str",
        "repeat": "",
        "crontab": "",
        "once": false,
        "x": 150,
        "y": 1229,
        "wires": [
            [
                "f378e359.0c872"
            ]
        ]
    },
    {
        "id": "f378e359.0c872",
        "type": "function",
        "z": "106a5b82.ef95a4",
        "name": "List all context.global properties",
        "func": "/*\nmsg.payload = {\n    mqttWsPort: context.global.mqttWsPort,\n    nrPort: context.global.nrPort,\n    'delim': context.global.path.delimiter\n};\n*/\n//msg.payload = context.global;\nmsg.topic = \"List/Context-Global\";\nmsg.payload = Object.getOwnPropertyNames(context.global).sort();\nreturn msg;",
        "outputs": 1,
        "x": 358,
        "y": 1229,
        "wires": [
            [
                "8ce935af.7316c8",
                "7bb54311.844abc"
            ]
        ]
    },
    {
        "id": "8ce935af.7316c8",
        "type": "debug",
        "z": "106a5b82.ef95a4",
        "name": "",
        "active": true,
        "console": "false",
        "complete": "false",
        "x": 628,
        "y": 1229,
        "wires": []
    }
]

You should be able to work the rest out from there. Object.keys gives you the properties, Object.values gives you the property values.

[quote="jovee, post:3, topic:12883"]
Thanks so much for the prompt reply!

My end goal is to filter out some of the key/value pairs that have been saved in context, and then shoot the smaller object out as a string via TCP.
Thanks so much for the prompt reply!

My end goal is to filter out some of the key/value pairs that have been saved in context, and then shoot the smaller object out as a string via TCP.

Would you mind explaining the lines at the top that are commented out? I see some things (mqttWsPort) that I'm not familiar with. Still getting my footing with a lot of js :slight_smile:

Sorry, I posted that rather quickly so didn't remove things specific to my config. The things you refer to are global variables that I set up in my settings.js file. I pass down some standard things like the IP address and port that I'm using. Not really needed.

The bit that is interesting is that you can, though generally shouldn't access the globals via the built-in context.global. While this is generally frowned upon and may not work forever, it does work at the moment.

No worries, thanks for clearing that up!

I'm still fumbling my way around creating a new object with the retrieved keys and values, but I'm sure it'll click sooner or later, as I continue to sharpen my googling.