Containing data in the global and then reusing it

Hi @jake123

Please stop pursuing this. It is never going to be the right way to do what you want. You are asking us to give up our own time to help you do something we have repeatedly told you is not the right way to do it.

I would much rather help you get back on track towards a better model for what you want: using configuration nodes to let the user provide server details that can be easily shared by multiple nodes. This is the model used by the MQTT nodes, the WebSocket nodes and many many others.

1 Like

Agree absolutely with the others here. If you want to know the details about how context variables work then you can look at how the API is structured and maybe even write your own handler.

One other thing that hasn't been mentioned specifically is that the global context can be accessed from any flow as well as from the Editor sidebar. That means that anything and anyone (with access to the editor) could come along and destroy your variable.

Custom nodes already have mechanisms built in that let you add to a list via the editor (with a re-deploy) or you can also manage the data at the back-end (so that someone could send a msg for example with a new IP in it - though be careful because that could be quite dangerous).

And if you want to learn how to more dynamically connect to the back-end to get data, a good starting point is the core serial node. There the back-end (the .js file) gets the available ports from the OS. The Editor front-end (the .html file) makes a web call to an API defined by the back-end. Lots of nodes use the same kind of mechanism including uibuilder.

1 Like

i get it . Im trying some other way now . Perserving the data in the array . Is there a way i can access the variable i set in JS file in my oneditprepare function?

Did you read the last paragraph from @TotallyInformation ?

If you really want the edit dialog to get data from the runtime, you need to create a custom admin http endpoint that you can call from the editor. Just as @TotallyInformation told you - you can look at the node-red-node-serialport node for how it passes the list of connected serial devices from the runtime to the editor.

This StackOverflow answer also provides some information - javascript - Send data on configuration - Stack Overflow

But I have to say, again, you are not going down the right path. If you want the user to provide the IP addresses later, then why keep pursuing options that have them hard-coded in the runtime side?

What about i try making an http request to fill down the dropdown.Ill have 1 server on for now. But if user wants to set his own ip i could maybe make another POST reqsuest to post the value and then next time the user opens the node it fills the dropdown with the ip he inputed?

Passing data from the Editor to the runtime is easy, the standard configuration variables in your panel will do that. It is only if you need dynamic, runtime information that you need to have an API call to the runtime.

So, for example, if some other, external process could add to the list of IP addresses.

If you only want users to be able to add them in the Node-RED editor, you don't need anything special, just define a list variable in the template. In the runtime .js file, Node-RED already passes those variables into your node and each instance of your node keeps its own permanent store of those variables. Typically (by convention), you transfer them from the config into a variable called node:

//...

    function nodeInstance(config) {
        // Create the node
        RED.nodes.createNode(this, config)

        /** Copy 'this' object in case we need it in context of callbacks of other functions. @type {uibNode}       */
        const node = this
        // Copy variables from the Editor config panel into the node instance
        node.name            = config.name  || ''
        //...
    }
//...
RED.nodes.registerType(moduleName, nodeInstance)
//...

Everything inside the function is per-node-instance. Anything outside of that is Node-RED global. There is another layer inside the instance code that is per-message (assuming you are allowing incoming messages, it isn't mandatory of course).

If you want to allow users to, lets say, add IP addresses from a Dashboard (e.g. not the Editor). Then this should be done via a flow and you will build something into the runtime to capture that from an incoming msg and store it into somewhere that will survive a reboot - the safest and easiest being a file. If that is the case, then you WILL want an API lookup because you now have an external process potentially changing the IP list and you need to get the latest version when you access the Editor panel.

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