List of all global

I am looking for a method to retrieve the name of the global variables as well as their value.

For the moment, I only manage to have an array with the values:
global.get (global.keys ())

I can't get the name of the variables the same way they are displayed in the context

Sans titre

Can anyone tell me how to do this?

Usually you know the global property you used to save the value in the first place and you retrieve it with
For example: let progress1 = global.get('progress1')

You could use a Function node to generate the list by looping through the keys

let list = []

global.keys().forEach( key => {
    list.push( { [key]: global.get(key)});

msg.payload = list
return msg

To use global.keys(), you need to have the following in settings.js:

exportGlobalContextKeys: true,

Since that is actually a bit of a security issue if you allow people access to the full list all the time.

Thats strange .. i checked my settings.js and indeed its set to exportGlobalContextKeys: false by default but the above code run regardless.

Node-RED version: v2.0.5
Node.js version: v14.16.0

Ah, interesting, is that a bug I wonder?

Thanks, it's exactly what I want :slight_smile:

@Xeniluom cool .. now that i think of it maybe it would be more easily accesible if the list was an object.

let list = {};

global.keys().forEach( key => {
    list[key] = global.get(key);

msg.payload = list
return msg

@TotallyInformation seems like it.

exportGlobalContextKeys controls whether the global context keys set via functionGlobalContext are exposed or not.

Any global context properties set in the flow itself will always be returned.


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.