Context global - node initialize

Dear community,

In our custom node, we want to save an connection into context.global value.
When i try this

node.initialize = () => {

        node.status({
            fill: "yellow",
            shape: "dot",
            text: "connecting"
        });
     
        if (!connection || connection.is_closed()) {
            connection = context.global.awsConnection 

I got an error "cannot get awsConnection of undefined". If we deploy again code with nodered, the function intialize works well. So is there any tips to wait until context.global is defined ?

I think you need to call context as a function...

https://nodered.org/docs/creating-nodes/context

and context is a property of this.
In a function node, context is available automatically

Why do you want to store a connection in Context? What problem does that solve for you?

T tried this.context().global but is undefined also.
I use context to share a connexion with multiple nodes.

Are those nodes all of the same type? In other words, are they defined in the same JS file? If so, you could just use a variable in your file to share them - rather than use context that is typically used for data that can be serialised as JSON and saved.

can you share more of your code?

But as Nick suggested, either store it in a variable within that js file, or, maybe even better, create a configuration node for configuring the connection

yes same type, sames js file.
I don't know why i didn't think before about your solution.

I will use internal variable in js file

1 Like

Just one more question about the same problem.
If it's two different jsfile, what solution would be the best ? Config node can be shared with diffente node ?

If those two nodes are provided by the same node.js module then you could add another js file to the module that both node files require to provide a common utility library they can both use.

1 Like

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