How to use global.set in my case

Hi there,

i like to use a function node to bring incomming messages to a global value.
Hard for me to discribe, as the problem seems to be very rare. Lets try:

My recent code:
var name = msg.channelName;
var status = msg.payload;
global.set('name', status)
return msg;

The "name" in the global.set context should be the var from above. And I have no idea how the right code shoud look like.

Any hints?

Best wishes
bibi

I don't see any problems here. If it stays that simple a normal "change" node would do the job too.
During testing be sure to refresh the display of globals on the right window. I had this stupid mistake once and where wondering about the status of my global variables.

Remove the quotes...

var name = msg.channelName;
var status = msg.payload;
//global.set('name', status); // << wrong
global.set(name, status); // << correct
return msg;

Changed to this:

var name = msg.channelName;
var status = msg.payload;
global.set(name, status);
return msg;

and getting this error:
Error: Invalid property expression: unexpected ' ' at position 10

Change code to this...

var name = msg.channelName;
var status = msg.payload;
node.warn("name = " + name );
node.warn("status = " + status );
global.set(name, status);
return msg;

what do you see in debug side bar?

oh stop. the "status" is sometimes boolean, sometimes a string (0 or 1). Maybe that ist why?

No, you can store bool/string/object/whatever in the global context.

It is the name that is important - it must be a non zero length string

Maybe because of the spaces in name?

no, spaces are valid in the name.

Are you still getting an error? Have you checked the context data side bar panel? Have you presed the refresh button?

image

yes, i did. Can you see the screenshot i uploaded from the sidebar? if not:

name = Schlafzimmer Fenster
status = 1

name = Wohnzimmer links Tür
status = 0

... and so on. Seems to be correct.

So it is working now?

Is there anything in the context data view like I showed

No :frowning:

Here is the second screen with the errors:

I thought that too, but testing, even with a fixed string, does give an error
global.set("some string", "test")
gives the error that @bibi-b is seeing (but at position 4)

Wow, could have sworn I tested that - you are right colin lol

@bibi-b you will have to remove spaces from the names OR use an object to store the values in then store the object.

Thank you both!

Do you have a quick code i can insert in the function node to change spaces to an underscore _ ?

Try this instead first - you might find it preferable to having hundreds of top level global variables - especially if you want to deleted them all in one go...

const channelData = global.get("channelData") || {};
const name = msg.channelName;
const status = msg.payload;
channelData[name] = status;
global.set("channelData", channelData);
return msg;

Working perfectly. Thanks a lot for your time and help!

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