How to set context variables containing spaces?

I know this isn't a huge deal and I could just use underscores, except that I'm positive I did this literally yesterday and now I've no idea how I accomplished it.

I'm trying to create the property "heat index" in the global context object, but I've tried the following to no avail:

global.set("[\"heat index\"]",hindex);

and

var tmp = "heat index";
global.set(tmp,hindex);

What am I missing? Just yesterday I created that context, changed my mind and today I'm trying to change it back, but I have no idea what I did to create it.

I even tried to change the global.json file, replacing the underscore with a space, which caused NodeRED to crash every time it tried to set up.

So now I'm wondering whether I only thought I accomplished that yesterday in my late night delirium and now I'm losing my mind trying to redo what can't be done...

You are right, there seems to be a bug when there are spaces in the key.

I just tried to set and get a context value named foo bar

global.set('foo bar', 123);
global.get('foo bar');

and ended up with the following error:

Error: Invalid property expression: unexpected ' ' at position 3"

After removing the space it worked.

You should open a ticket here.

EDIT:

I believe the key is evaluated as an expression, so this may be a feature rather than a bug. :sweat_smile:

That being said... if you really need to use keys with spaces

global.set('\'foo baz\'', 456);

should do the trick.

However, I found that it will not show up in the context sidebar, if there are any spaces in the key.

Global variables should be considered just that, a variable rather than a key to a variable. In that case, a space isn't valid so I'm guessing that's why it hasn't been implemented.

Just create a variable name without the space and if you need something with a space, add it to that variable as an object instead.

But you can't use global 'variables' as variables... You can neither assign them nor retrieve them as you would a variable, you must .get() and .set() all context variables or the changes will not persist between nodes.

For example, let's string two function nodes together. The first node is:

// Set test1 like a variable
global.test1 = true;
// Set test2 like a property
global.set("test2",true);
return msg;

Second node:

var newMsg = {
    // Retrieve both like variables
    "variable": {
        "test1": global.test1,
        "test2": global.test2
    },
    // Retrieve both like properties
    "property": {
        "test1": global.get("test1"),
        "test2": global.get("test2")
    }
};
return newMsg;

This returns the following object:

{
    "variable": {},
    "property": {
        "test2": true
    },
    "_msgid": "1e2b86c4.b17c99"
}

Even in change nodes it's immediately apparent that they're object properties and not variables.

I only wanted to improve the look of the context panel, since I'm currently using NodeRED as my dashboard, where only the top-level properties are listed in plaintext. It's just frustrating that the devs have set arbitrary JSON formatting rules.

  1. Inside a function the deprecated way of accessing was

    context.global.test1 = true

Not global.test1

  1. we have always said the editor is NOT a dashboard