# Context global - node initialize

**URL:** <https://discourse.nodered.org/t/context-global-node-initialize/28182>\
**Category:** Developing Nodes\
**Created:** [11 June 2020 14:37 UTC](https://discourse.nodered.org/t/context-global-node-initialize/28182 "2020-06-11T14:37:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sragneau](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sragneau/32/23768_2.png) [@sragneau](https://discourse.nodered.org/u/sragneau)\
**Post date:** [11 June 2020 14:37 UTC](https://discourse.nodered.org/t/context-global-node-initialize/28182/1 "2020-06-11T14:37:33Z")

</div>

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 ?

---

<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:** [11 June 2020 14:53 UTC](https://discourse.nodered.org/t/context-global-node-initialize/28182/2 "2020-06-11T14:53:15Z")

</div>

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

[https://nodered.org/docs/creating-nodes/context](https://nodered.org/docs/creating-nodes/context)

---

<div class="post-metadata">

**Author:** ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)\
**Post date:** [11 June 2020 15:04 UTC](https://discourse.nodered.org/t/context-global-node-initialize/28182/3 "2020-06-11T15:04:43Z")

</div>

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

---

<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:** [11 June 2020 15:05 UTC](https://discourse.nodered.org/t/context-global-node-initialize/28182/4 "2020-06-11T15:05:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sragneau](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sragneau/32/23768_2.png) [@sragneau](https://discourse.nodered.org/u/sragneau)\
**Post date:** [11 June 2020 15:37 UTC](https://discourse.nodered.org/t/context-global-node-initialize/28182/5 "2020-06-11T15:37:35Z")

</div>

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

---

<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:** [11 June 2020 15:38 UTC](https://discourse.nodered.org/t/context-global-node-initialize/28182/6 "2020-06-11T15:38:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cinhcet](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cinhcet/32/438_2.png) [@cinhcet](https://discourse.nodered.org/u/cinhcet)\
**Post date:** [11 June 2020 15:41 UTC](https://discourse.nodered.org/t/context-global-node-initialize/28182/7 "2020-06-11T15:41:41Z")

</div>

> [@sragneau](#):
>
> T tried this.context().global but is undefined also.

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

---

<div class="post-metadata">

**Author:** ![sragneau](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sragneau/32/23768_2.png) [@sragneau](https://discourse.nodered.org/u/sragneau)\
**Post date:** [11 June 2020 15:42 UTC](https://discourse.nodered.org/t/context-global-node-initialize/28182/8 "2020-06-11T15:42:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sragneau](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sragneau/32/23768_2.png) [@sragneau](https://discourse.nodered.org/u/sragneau)\
**Post date:** [11 June 2020 16:00 UTC](https://discourse.nodered.org/t/context-global-node-initialize/28182/9 "2020-06-11T16:00:49Z")

</div>

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 ?

---

<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:** [11 June 2020 16:04 UTC](https://discourse.nodered.org/t/context-global-node-initialize/28182/10 "2020-06-11T16:04:10Z")

</div>

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.

---

<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:** [10 August 2020 16:04 UTC](https://discourse.nodered.org/t/context-global-node-initialize/28182/11 "2020-08-10T16:04:11Z")

</div>

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