# Containing data in the global and then reusing it

**URL:** <https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748>\
**Category:** Developing Nodes\
**Created:** [26 April 2021 13:12 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748 "2021-04-26T13:12:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [26 April 2021 13:12 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/1 "2021-04-26T13:12:14Z")

</div>

Hello, I want some data to be contained in global . I want it to be multiple ip adressess that i set manually. And then for each ip address in the global i want to create a option tag. Is that somehow possible and if yes how would i do it?

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [26 April 2021 13:30 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/2 "2021-04-26T13:30:52Z")

</div>

One possibility would be to store the information in a JSON object:

```auto
[{"id":"3be06c00.110564","type":"inject","z":"b8546237.5f9b8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":320,"wires":[["c07eb398.3d1d2"]]},{"id":"c07eb398.3d1d2","type":"change","z":"b8546237.5f9b8","name":"","rules":[{"t":"set","p":"test","pt":"global","to":"{\"IP-1\":\"192.168.1.1\",\"IP-2\":\"192.168.1.2\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":340,"wires":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 April 2021 13:32 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/3 "2021-04-26T13:32:05Z")

</div>

Hi @jake123

from our previous conversations, I have the benefit of having a bit of context to this question, but even then, I'm not entirely sure what you mean.

When you say 'global' do you mean in the JavaScript sense of the global namespace in the browser, or do you mean Node-RED's Global context?

When you say you want to create an option tag, where is that? In a node's edit dialog? On a webpage your flow is creating? On site created using node-red-dashboard?

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [26 April 2021 13:33 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/4 "2021-04-26T13:33:52Z")

</div>

> [@ghayne](#):
>
> `3be06c00.11`

I want to store the data in the global context of node red . And then im my nodes edit dialog i want to have and select tag filled with options . I would generate those options using a loop through all the data i have stored in the global context and for each i would create an option tag.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 April 2021 13:36 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/5 "2021-04-26T13:36:38Z")

</div>

> [@jake123](#):
>
> I want to store the data in the global context of node red

Node's do not typically use context in their edit dialog - because you have the problem that it takes a running flow to update context. So the first time they add an instance of your node, there is nothing running to have generated anything in context.

So in your scenario, how does the data get into context to begin with?

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [26 April 2021 13:45 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/6 "2021-04-26T13:45:00Z")

</div>

Imagine something like this

I want to have this in my js file.

```auto
  let options= node.options;
          options= [];
          let ip1 = "192.1.1.1";
          let ip2 = "192.2.2.2";
          options.push(ip1)
          options.push(ip2)

```

And then something like this in my oneditprepare function.

```auto
let options= $("#node-input-options")
for (let i = 0; i < options.length; i++) {
let value = options[i].v;
let text = options[i].t;
$("#node-input-options").append(<option></option>).attr("value", value).text(text)
}

```

I know how to make a dropdown and all of that stuff. This is just a glimpse in what i want . The code for the dropdown is probalby wrong now but i knwo how to do it. I have the problem with setting global context and then using its data. If its even possible in the way i want it

---

<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:** [26 April 2021 13:57 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/7 "2021-04-26T13:57:06Z")

</div>

Jake, are you 100% certain you understand what `global` context (in node-red terms) is?

It is server side store (NOT client side / not Browser) that stores values - most often set by a change node. for example...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/f/df977f8c67dc88ccaba7b12251834bc140bf3dc7.png)

I just want to be 100% sure that is what you are talking about.

* * *

> [@jake123](#):
>
> I want to have this in my js file.
> 
> ```auto
> let options= node.options;
> options= [];
> let ip1 = "192.1.1.1";
> let ip2 = "192.2.2.2";
> options.push(ip1)
> options.push(ip2)
> 
> ```

Q. Why would you hard code IP addresses in the serverside js? (or the client side script for that matter)

Q. Why do you set `let options= node.options` then immediately overwrite it with `options= [];` ?

* * *

Are you certain you dont simply want values set/selected at the client side to be available in your server side code?

It is not clear at all what you are trying to achieve.

* * *

PS: Writing to global context from within in your node is really **not** a great idea & you probably dont need it - if you can clearly explain what & why you need this, we can likely steer you to a better solution.

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [26 April 2021 14:00 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/8 "2021-04-26T14:00:52Z")

</div>

This is all just scrappy code with which i try to explain what i want. I want to have some ip addresses stored in the global context of node red. Then i want to populate the select tag with all of those ips in global context. For now its hardcoded later it wont be . I just dont understand how can i use the global context to populate the select tag.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 April 2021 14:06 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/9 "2021-04-26T14:06:17Z")

</div>

> [@jake123](#):
>
> I want to have some ip addresses stored in the global context of node red.

Why do you want it in Global Context? That isn't a place you should be storing Node configuration.

You say it won't be hardcoded in the future - so how will it be set? Does the user provide those IP addresses somehow?

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [26 April 2021 14:07 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/10 "2021-04-26T14:07:51Z")

</div>

Ill have a button to add new ips so if the user wants to use some ip later on he doesnt have to write it all over again

---

<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:** [26 April 2021 14:09 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/11 "2021-04-26T14:09:36Z")

</div>

> [@jake123](#):
>
> Ill have a button to add new ips

where would this button be - in your nodes edit page?

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [26 April 2021 14:13 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/12 "2021-04-26T14:13:32Z")

</div>

OK let me explain it a bit easier. Let say this is my ip address

```auto
let ip = 191.1.1.1;
let globalContext = this.context().global;
globalContext.set("Ip", ip)

```

if thats the right way to do it how would i now use this in my html fil to populate the dropdown.  
And yes the button will be in node edit dialog

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 April 2021 14:14 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/13 "2021-04-26T14:14:50Z")

</div>

> [@jake123](#):
>
> if thats the right way to do it how would i now use this in my html file.

Okay. Let's make this super clear. You do _not_ use global context to store node configuration.

---

<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:** [26 April 2021 14:15 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/14 "2021-04-26T14:15:42Z")

</div>

ok, so as I suspected and as Nick mentioned, you want to set context from your node - this is just such a really really bad idea.

There are other solutions. for example, you could have a control topic where the user injects the IP addresses into your node - completely avoiding use of context.

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [26 April 2021 14:16 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/15 "2021-04-26T14:16:58Z")

</div>

Yeah i get that. Before i had an simple input and the user writes the ip and it automaticlly connects to the ip. But now just for traning purposes i want to explore the global context and how to use it from withing the node to create something. In this case a dropdown of its data

---

<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:** [26 April 2021 14:41 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/16 "2021-04-26T14:41:03Z")

</div>

> [@jake123](#):
>
> just for traning purposes i want to explore the global context and how to use it from withing the node to create something

oneditprepare is "client side"  
global context is "server side"

So you need a way of "getting" the values from the serverside runtime to the browser (and back again).

This is typically done by creating an endpoint in your nodes .js file and calling the endpoint from your .html files script.

# BUT

If it is only for training - lets pretend it is now 4 days later and you have now got it working - then you realise it was a bad idea & now understand why it was **STONGLY** discouraged 🙂

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [26 April 2021 14:41 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/17 "2021-04-26T14:41:41Z")

</div>

Is it even possible to access the global context in oneditprepare function?

---

<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:** [26 April 2021 14:42 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/18 "2021-04-26T14:42:13Z")

</div>

> [@jake123](#):
>
> Is it even possible to access the global context in oneditprepare function

No, not directly...

> [@Steve-Mcl](#):
>
> oneditprepare is "client side"  
> global context is "server side"

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [26 April 2021 14:42 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/19 "2021-04-26T14:42:35Z")

</div>

yeah i didnt see u replyed to the post before 😃

---

<div class="post-metadata">

**Author:** ![jake123](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jake123](https://discourse.nodered.org/u/jake123)\
**Post date:** [26 April 2021 14:43 UTC](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748/20 "2021-04-26T14:43:07Z")

</div>

could u somehow explain what would be that endpoint?

[Next page](https://discourse.nodered.org/t/containing-data-in-the-global-and-then-reusing-it/44748.md?page=2)
