# Global context probems

**URL:** <https://discourse.nodered.org/t/global-context-probems/3038>\
**Category:** General\
**Created:** [11 September 2018 11:01 UTC](https://discourse.nodered.org/t/global-context-probems/3038 "2018-09-11T11:01:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fil](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@fil](https://discourse.nodered.org/u/fil)\
**Post date:** [11 September 2018 11:01 UTC](https://discourse.nodered.org/t/global-context-probems/3038/1 "2018-09-11T11:01:51Z")

</div>

I have a need to access variables common to all flows, i am creating/setting the values in a dashboard flow devoted to ui and wish to access the values set (or their default) in flows that perform tests and stimulate further action conditionally.

when looking up how to do this i found [Node context : Node-RED](https://nodered.org/docs/creating-nodes/context)  
but when i tried to declare variables in that format with the keyword 'this' i got errors, which led me to find posts on the google group suggesting that the node red functions page detailed everything i needed to know. so i have been back there, [Redirecting…](https://nodered.org/docs/writing-functions)

which says

> ```
> Global context
> 
> ```
> 
> The global context **can** be pre-populated with objects when Node-RED starts. This is defined in the main settings.js file under the functionGlobalContext property.

So I dont Have to declare and predefine them in that file, i can just add a '||DefaultVal' suffix to my global.get()??

my recollection of the google trail is sketchy but i seem to recall reading that the page referring to the 'this' object was out of date, but the page it links to discusses release 0.19.. but its an official node red doc page??

so in my ui flow i am using  
var P1MML = global.get('P1MML')||30;  
global.set('P1MML',msg.payload);  
return msg;

and in my other flows accessing the global  
`P1MML = global.get('P1MML')||30;`

anything else needed???

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [11 September 2018 11:19 UTC](https://discourse.nodered.org/t/global-context-probems/3038/2 "2018-09-11T11:19:51Z")

</div>

OK, you have confused two different contexts. The first link is a reference to creating custom nodes. But then you talk about the function node.

In a function node, you have access to three types of variable. "context" is only available to that specific function node instance. "Flow" is only available to nodes in the "tab" in the admin ui that your node is in. Global is available to everything.

If you want to pre-populate a variable, you either have to trigger setting it when Node-RED starts or, but only for globals, you can pre-define it in the settings.js file. But you don't have to do that. As long as you allow in your flows for a global/flow/context variable to not yet existing, that's fine.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [11 September 2018 11:22 UTC](https://discourse.nodered.org/t/global-context-probems/3038/3 "2018-09-11T11:22:58Z")

</div>

The first page is about creating you own node-red-contrib-nodes (contains creating-nodes within the URL) The second page is if you are writing functions or using the context settings within a change node (contains writing-functions within the URL).

in v0.19 there are different ways of storing context values which means they can be prepopulated.

Is the third page you read an official node-RED doc page? Well without the URL that would be tricky to answer

But what you have written should work, so have you tried it?

---

<div class="post-metadata">

**Author:** ![fil](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@fil](https://discourse.nodered.org/u/fil)\
**Post date:** [11 September 2018 11:24 UTC](https://discourse.nodered.org/t/global-context-probems/3038/4 "2018-09-11T11:24:53Z")

</div>

Im far from creating custom nodes, just embedding my global settings in some new code and things are not quite right and when checking on what i may have missed i found the above rabbit hole

time to pull thngs apart and test individually
