Containing data in the global and then reusing it

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?

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

[{"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":[[]]}]

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?

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.

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?

Imagine something like this

I want to have this in my js file.

  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.

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

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

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


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.

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.

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?

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

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

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

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

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

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.

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

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 :slight_smile:

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

No, not directly...

yeah i didnt see u replyed to the post before :smiley:

could u somehow explain what would be that endpoint?