Change node documentation wrong?

Folks,

I'm trying to use a change node to get a global context value and not having luck.

Looking at:
how to get global context in change node

And I now see that it has been answered as I had fallen for this in the past.
That is NOT the problem.

The documentation is not correct.
Now I'm saying the documentation is WRONG.

What I need is J: $globalContext('name')

But the link above says:

To: J: global.get('myGlobalVar')

Which doesn't work.

SHOULD IT BE CHANGED?
By whom?

Of course you are right that the jsonata syntax which works is $globalContext('name')

global.get('myGlobalVar') is javascript syntax and works in a function node.

Your link gives me a google search reply, it;s not clear which actual document has the bad advice?
But the sentence you searched for node-red change node javascript global context is ambiguous because the J: option in the change node is for Jsonata not javascript .

One more thing - why can't you access the context variable using the "global" dropdown? Maybe you are intending a more complex Jsonata operation upon the returned value?

Ok, thanks.

I was wanting to .... modify the value. Not just get it.

So I wanted twice the global value.

Which can't be done natively in the change node.

So you have to go to the J: mode.

So it would be J: global.get('name') *2) - for instance.

But that gives me nothing.

So I dug around and found that I have to do it as shown:
J: $global.get('name') * 2

And that works.

Sorry I didn't qualify that when I posted. I'm sorting stuff and this is the background task.

I too have occasionally used jsonata expressions in a change node for simple calculations.
I always struggle to make it work.

Jsonata is undoubtedly powerful but the syntax is just too obscure for me.

These days I'm more likely to use javascript in a function node.
I'm no expert on javascript but at least I stand a chance of getting it to work!

3 Likes

I am only mentioning this as in the past when I've used function nodes to do such things, people have suggested I use he change node - as it is more light weight for doing that sort of thing.

But now - it seems - some of the workings under the hood have changed.

MAYBE the original would work if I was not doing anything more than simply getting the value.

But as the second way (J: $global.get('name') *2) works (be it with or without the * 2 bit....
Standardise it to be the way documented then if the future when people increase their need on what it does: they don't need to learn a new structure.

:person_shrugging:

Just a thought.

All good though.

Well maybe it is but I'm not convinced!

That syntax does not work in a change node for me.

This


gives me undefined

while this


gives me 198

I'm doing MR 4.0.9

You?

Sorry, just so we both know.

SORRY!

Ok, we do both agree.

That's what I'm saying.

The docs say the one that doesn't work.

So I think they should be corrected ASAP

What docs?

Well, this is what I am seeing.

It is written there.

Sorry, I didn't scroll down far enough.

Try this one.

Just a bit lower in the text from what I just posted/showed.

That is talking about a Function node, which uses javascript. Also that is not node-red documentation, that is google AI.

Not how I read the page.

At the top it is talking about the change node.

Then it goes to how to access contexts... (yech, bad word)

Ok: my bad.

I probably shouldn't trust google AI.

SORRY FOLKS.

The hint at the top of your fist image is "AI Overview" which translates to "made up bull shit, please ignore"

5 Likes

Read the words, it is perfectly correct, it says:
'You can also access global context variables within a function node using ...'
Function node, not Change node.

I think that is going too far, I think better would be 'take it with a pinch of salt`. In fact it is usually frighteningly accurate.

1 Like

Well, thanks all....

I have got it working.

I just wanted to check on the validity of the documentation.

Oh, just to add:

[{"id":"31a9067d8b0ac069","type":"inject","z":"65c9b63cb09879a0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":780,"y":500,"wires":[["fb7426b3870c451a"]]},{"id":"fb7426b3870c451a","type":"change","z":"65c9b63cb09879a0","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"test","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":970,"y":500,"wires":[["0d935bbe350562b5"]]},{"id":"0d935bbe350562b5","type":"debug","z":"65c9b63cb09879a0","name":"debug 11","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1170,"y":500,"wires":[]},{"id":"61e1fd3f6a0ed1d1","type":"change","z":"65c9b63cb09879a0","name":"","rules":[{"t":"set","p":"test","pt":"global","to":"20","tot":"num"},{"t":"set","p":"payload","pt":"msg","to":"$globalContext('test') * 2","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":970,"y":570,"wires":[["f083ffb87d2e9be0"]]},{"id":"5b5b153ff9a3c119","type":"inject","z":"65c9b63cb09879a0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":780,"y":570,"wires":[["61e1fd3f6a0ed1d1"]]},{"id":"f083ffb87d2e9be0","type":"debug","z":"65c9b63cb09879a0","name":"debug 12","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1170,"y":570,"wires":[]},{"id":"89eb6a476a8c3345","type":"inject","z":"65c9b63cb09879a0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":780,"y":450,"wires":[["71aeac5dd2337c08"]]},{"id":"71aeac5dd2337c08","type":"change","z":"65c9b63cb09879a0","name":"","rules":[{"t":"set","p":"test","pt":"global","to":"44","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":970,"y":450,"wires":[[]]}]

BOTH of these work. Given I do the first inject before trying to use the second one the first time around - or..... that global:test is set.

"made up bull shit, please ignore"

Unless you are living under a rock, you are gravely mistaken, plus that it is 100% correct with its response.

Come to think of it (offtopic) - why does the node-red repo not have a AI-bot to help out with the issues and PR's ? I have come across several repo's where issues are being picked up by AI bots with auto-PR's.

It is indeed correct when it says
Screenshot 2025-06-01 at 13.36.51

1 Like

Though that is of course a universal truth, not limited to AI.
Even the official node-red docs no doubt have some mistakes

As other have said this is an AI response, so nothing to do with Node-Red docs, BUT it reads correct to me anyway ?

Come to think of it (offtopic) - why does the node-red repo not have a AI-bot to help out with the issues and PR's ? I have come across several repo's where issues are being picked up by AI bots with auto-PR's.

And you are volunteering to check and test all this AI generated code?

But sometimes merely frightening