How to get get all global variables?

I need to get all global context variables. Is there any function to get all global variable?

1 Like

hi @sathishkumarc14,
have you tried change node?

OR

using function node
https://nodered.org/docs/user-guide/writing-functions#global

something like below piece of code.
var xpto = global.get("xpto");
var xpto2 = global.get("xpto2");

There is a setting in the settings.js file that exposes the top-level of global and so lets you get a catalogue of all of the variables. Sorry, the details escape me right now but it is in the docs I'm sure.

1 Like

Good question. I am not aware of a way to get all the context with a single function.

As @TotallyInformation mentioned you can do a change in settings.js , more specifically setting the property exportGlobalContextKeys to true. This will allow you to get a list of keys with the function global.keys(). Such list may be used to programmatically retrieve all the context. Another alternative is to read the files that hold the context persisted in the filesystem. In a raspberry pi you can find these files in the folder /home/pi/.node-red/context/

Code example:

let keys = global.keys();

for (let k of keys) {
    node.warn("Key = " + k);
    node.warn(global.get(k));
}

return msg;
2 Likes

Thank you all for great information.

I forgot that it is possible to get multiples values in a single get statement. which results that it is possible to use a single statement in a function node to get all the global context variables.

msg.payload = global.get(global.keys());

The result is an array with each and every object stored in the global context.

2 Likes

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