# How to use global.set in my case

**URL:** https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629
**Category:** General
**Created:** [25 June 2021 07:59 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629 "2021-06-25T07:59:37Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![bibi-b](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@bibi-b](https://discourse.nodered.org/u/bibi-b)
#### Post date: [25 June 2021 07:59 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/1 "2021-06-25T07:59:37Z")

</div>

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

---

<div class="post-metadata">

### Author: ![haegar33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haegar33/32/38805_2.png) [@haegar33](https://discourse.nodered.org/u/haegar33)
#### Post date: [25 June 2021 08:03 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/2 "2021-06-25T08:03:47Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [25 June 2021 08:05 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/3 "2021-06-25T08:05:02Z")

</div>

> [@bibi-b](#):
>
> 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.

Remove the quotes...

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

```

---

<div class="post-metadata">

### Author: ![bibi-b](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@bibi-b](https://discourse.nodered.org/u/bibi-b)
#### Post date: [25 June 2021 08:08 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/4 "2021-06-25T08:08:00Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [25 June 2021 08:09 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/5 "2021-06-25T08:09:28Z")

</div>

Change code to this...

```auto
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?

---

<div class="post-metadata">

### Author: ![bibi-b](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@bibi-b](https://discourse.nodered.org/u/bibi-b)
#### Post date: [25 June 2021 08:09 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/6 "2021-06-25T08:09:47Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [25 June 2021 08:10 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/7 "2021-06-25T08:10:44Z")

</div>

> [@bibi-b](#):
>
> 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

---

<div class="post-metadata">

### Author: ![bibi-b](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@bibi-b](https://discourse.nodered.org/u/bibi-b)
#### Post date: [25 June 2021 08:11 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/8 "2021-06-25T08:11:51Z")

</div>

![Bildschirmfoto 2021-06-25 um 10.10.32](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/f/9f6abbc3dd8a693d7d9da0ac57f2416a37007839.png)

Maybe because of the spaces in name?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [25 June 2021 08:13 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/9 "2021-06-25T08:13:24Z")

</div>

> [@bibi-b](#):
>
> 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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/ed3cdbc8be494bfb0a797e847bdc28233e807e0e.png)

---

<div class="post-metadata">

### Author: ![bibi-b](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@bibi-b](https://discourse.nodered.org/u/bibi-b)
#### Post date: [25 June 2021 08:16 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/10 "2021-06-25T08:16:43Z")

</div>

> [@Steve-Mcl](#):
>
> non zero length string

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.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [25 June 2021 08:17 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/11 "2021-06-25T08:17:56Z")

</div>

> [@bibi-b](#):
>
> Seems to be correct

So it is working now?

Is there anything in the **context data** view like I showed

> [@Steve-Mcl](#):
>
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/ed3cdbc8be494bfb0a797e847bdc28233e807e0e.png)

---

<div class="post-metadata">

### Author: ![bibi-b](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@bibi-b](https://discourse.nodered.org/u/bibi-b)
#### Post date: [25 June 2021 08:19 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/12 "2021-06-25T08:19:27Z")

</div>

No ☹

Here is the second screen with the errors:

 ![Bildschirmfoto 2021-06-25 um 10.18.32](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/a/4a2cfc3b4a432fb9d189c9412a63642906de4edb.png)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [25 June 2021 08:19 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/13 "2021-06-25T08:19:33Z")

</div>

> [@Steve-Mcl](#):
>
> no, spaces are valid in the name.

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)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [25 June 2021 08:21 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/14 "2021-06-25T08:21:45Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![bibi-b](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@bibi-b](https://discourse.nodered.org/u/bibi-b)
#### Post date: [25 June 2021 08:24 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/15 "2021-06-25T08:24:17Z")

</div>

Thank you both!

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

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [25 June 2021 08:27 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/16 "2021-06-25T08:27:34Z")

</div>

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...

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

```

---

<div class="post-metadata">

### Author: ![bibi-b](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@bibi-b](https://discourse.nodered.org/u/bibi-b)
#### Post date: [25 June 2021 08:29 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/17 "2021-06-25T08:29:56Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> 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!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [24 August 2021 08:30 UTC](https://discourse.nodered.org/t/how-to-use-global-set-in-my-case/47629/18 "2021-08-24T08:30:06Z")

</div>

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