How to just change the global variable name once

I use the config node node-red-contrib-config to declare global variables like so:

node-config-1

And in function node I get the value of the global aaa variable by global.get('aaa')

[{"id":"d1a34c16.5a144","type":"config","z":"aa40b590.ff1888","name":"","properties":[{"p":"aaa","pt":"global","to":"AAA","tot":"str"}],"active":true,"x":1740,"y":3020,"wires":[]},{"id":"f6f15e86.bd382","type":"function","z":"aa40b590.ff1888","name":"","func":"var value = global.get('aaa');\nmsg.payload = value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1740,"y":3080,"wires":[["53fe2cd9.4d1274"]]},{"id":"977a972b.ea5e18","type":"inject","z":"aa40b590.ff1888","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1590,"y":3080,"wires":[["f6f15e86.bd382"]]},{"id":"53fe2cd9.4d1274","type":"debug","z":"aa40b590.ff1888","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1910,"y":3080,"wires":[]}]

The problem I am having is:
in case there are multiple function nodes using the same global aaa variable, and get the value by global.get('aaa') as above,
then if I change the variable name aaa in config node (suppose to become bbb) then I have to go to all function nodes to change the variable name aaa of global.get('aaa') to global.get('bbb')

This is inconvenient, is there any way I can use a global variable that when changing the variable name, I only need to change the variable name once?

Sorry, but I must be missing something because I can't really see the need for that node since a change node (which is a core node) will do the job anyway.

Well, you could use another global variable to store the variable name :sunglasses:

But realistically, it is easier not to change the name once you've used it. If you were really desperate, you could shut down Node-RED and use a tool such as AWK or a code editor to do a search and replace in the flow file (having first taken a backup copy of course).

It is for this reason that I tend to use longer rather than shorter variable names (e.g. 10-20 chars is generally a lot better than 3-5) and try to make them as descriptive as possible.

1 Like

Could you maybe explain what you're trying to do by changing the variable name please? I can't really think of a use-case for this, and it seems a bit like renaming a variable in your function code :

var value = global.get('aaa');
...
rename value = newvalue;
...
msg.payload = newvalue;
return msg;

What is the reason for wanting to rename the global variable? As @TotallyInformation says, it's a good idea to give all variables, whether environment ones or in function code, meaningful names which describe their purpose and don't conflict with others.

1 Like

Thanks for the instruction, since i don't know a change node can do the same thing, i will use change node.

Even using a change node, the thing as I described above cannot change the global variable name once, right?

This is really complicated, right :blush:

I don't know the AWK tool, can you tell me a little more clearly?

Thank you for the advice, but this is just a description to make it easy to understand, I did not actually name this variable ^^

Since the global variable naming was erroneous in the beginning, I wanted to change it...

1 Like

If the name you used is a unique string then you might get away with renaming every occurrence of that string in the flows file. Make a copy of it first (it is in your .node-red directory will probably be called flows_<server_name>.json) in case of disasters. Stop node-red, use your favourite text editor or command line s/w (that is what awk was referring to) and replace all occurrences with the new name. Check them as it does them to make sure it has not obviously picked up an instance that you did not intend. Then restart node-red and see if all is well. If you don't understand then you would be quicker just to change them all manually. You can use the Search feature in the node-red editor to find them.

1 Like

Yes, a jest, not a real suggestion.

awk is a Linux/UNIX command line tool. If you don't already know it, forget it and use a text editor. But honestly, if you aren't sure what you are doing with the flow file, leave it alone. You will certainly break it from time to time if you do this.

Yes, I understood that but the advice remains - give variables a good name to start with and don't mess with them. If, occasionally, you have to anyway, accept the pain as a lesson to name things better for next time.

3 Likes

Thank everyone very much!

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