# Global variable undefined in subflow?

**URL:** https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132
**Category:** General
**Created:** [24 March 2021 17:03 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132 "2021-03-24T17:03:43Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)
#### Post date: [24 March 2021 17:03 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/1 "2021-03-24T17:03:44Z")

</div>

Hi All,

I've just bumped into a problem and I just can't believe my eyes. One of our systems started to send emails with undefined values where no such values should appear. So I started to investigate the email sending subflow and added a filter condition with a node.warn() to log it and also checked the global context variable in the editor.

This is how it looks like in the editor:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/b/7b60cb4aa8aba62e320cf9f37eac37946a7d7491.png)

And this is what I get in the syslog from node.warn():

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/ca54020789d25f878afe18d0c7932b56ee9c5edd.png)

What you should notice is that the global variable 'farmName' has a value in the editor when checking the global context but it is undefined when it got logged by the subflow.

Does anyone have any idea? This system has recently been upgraded to 1.2.9. Previously, it was running fine on 1.1.3.

Best regards,  
r0ller

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [24 March 2021 17:21 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/2 "2021-03-24T17:21:58Z")

</div>

Are you using multiple context stores? If so are you looking at the correct store in the editor?

---

<div class="post-metadata">

### Author: ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)
#### Post date: [24 March 2021 17:23 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/3 "2021-03-24T17:23:36Z")

</div>

Well, I don't think so 🙂 How can I check if multiple context stores are used?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [24 March 2021 17:24 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/4 "2021-03-24T17:24:31Z")

</div>

Look in `settings.js` for the bit about context.

---

<div class="post-metadata">

### Author: ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)
#### Post date: [24 March 2021 17:29 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/5 "2021-03-24T17:29:54Z")

</div>

If it's related to the contextStorage property, then it's in a comment block so it should not play any role here.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [24 March 2021 17:34 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/6 "2021-03-24T17:34:54Z")

</div>

Do you mean the contextStorage section is entirely commented out? I find that surprising but maybe it is ok. Are you looking at the file in your .node-red folder.

I guess that isn't the problem anyway, I couldn't think of any other reason it might be undefined.

Is the global var regularly written to? If so is it possible that it is has been written to with undefined but then gets returned to a sensible value before you refresh the view in the editor? It might be worth tracking down all places it is written to and add traps to check that it is not writing undefined.

**[Edit]** just a check, do you realise the view in the editor only updates when you click the refresh button?

---

<div class="post-metadata">

### Author: ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)
#### Post date: [24 March 2021 17:44 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/7 "2021-03-24T17:44:21Z")

</div>

Yepp, the complete section is commented out. I'm 100% sure that I checked the right file in the right folder.

That global variable is only set once when nodered starts up and never again. I double checked it now by searching for:  
global.set("farmName  
global.set('farmName

and the editor gives only that one function which is connected to the inject that triggers only on startup.

Refreshing the global context in the editor does not change anything with regards to that variable.

I also tried reading that variable simply by a manual inject node which I connected to a debug node and the value is correctly read. However, that I could only check on a normal tab not in the subflow's tab -that's why I suspect that it has to do something with being read in a subflow.

---

<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: [24 March 2021 17:46 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/8 "2021-03-24T17:46:53Z")

</div>

> [@r0ller](#):
>
> What you should notice is that the global variable 'farmName' has a value in the editor when checking the global context but it is undefined when it got logged by the subflow.
> 
> Does anyone have any idea? This system has recently been upgraded to 1.2.9. Previously, it was running fine on 1.1.3.

Are you certain the variable doesn't have a space at the beginning or end?

You can use `global.keys()` to get a list of keys in global - send that out to a debug node and inspect them

e.g. inject --\> function node --\> debug node

Function code...

```auto
msg.payload = global.keys();
return msg;

```

---

<div class="post-metadata">

### Author: ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)
#### Post date: [24 March 2021 17:53 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/9 "2021-03-24T17:53:30Z")

</div>

I also suspected that but already ruled out. Checking all the keys gives:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/f/ef70b0274ddbeb658c713715a6cdafc1d6b8b782.png)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [24 March 2021 20:34 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/10 "2021-03-24T20:34:29Z")

</div>

Does that global always show undefined in the subflow, or is it intermittent?  
Are there any other global vars used in the subflow?

---

<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: [24 March 2021 20:42 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/11 "2021-03-24T20:42:04Z")

</div>

> [@r0ller](#):
>
> This is how it looks like in the editor:
> 
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/b/7b60cb4aa8aba62e320cf9f37eac37946a7d7491.png)
> 
> And this is what I get in the syslog from node.warn():
> 
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/ca54020789d25f878afe18d0c7932b56ee9c5edd.png)

What I don't understand is there is more to that warn statement than just getting the global.

Can you try `node.warn(["farmName", global.get("farmName")])`

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [24 March 2021 21:09 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/12 "2021-03-24T21:09:48Z")

</div>

Perhaps you are trying to access the parent context of the subflow ? If this is the case you should test prepending `$parent` to the name of the scope variable.

> [@Subflow context (flow)](https://discourse.nodered.org/t/subflow-context-flow/11534/2):
>
> The nodes in a subflow can use flow context, bit it is scoped to the nodes in the subflow, not the flow the instance of the subflow is on. If you are on 0.20, then the nodes in the subflow can access the parent flow context. If you have a flow context variable called foo then the nodes in a subflow can use $parent.foo to access it.

---

<div class="post-metadata">

### Author: ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)
#### Post date: [25 March 2021 08:55 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/13 "2021-03-25T08:55:02Z")

</div>

There are a couple of others that are undefined as well which are also set only on startup. But I noticed that there's one (which is also set on startup only) and it's not empty. Funnily enough that's the variable that holds the email addresses that's why the email gets sent but with undefined values in it 🙂

---

<div class="post-metadata">

### Author: ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)
#### Post date: [25 March 2021 08:58 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/14 "2021-03-25T08:58:32Z")

</div>

I don't know why it appears as if there were more node.warns() either. I suspected that's because of the first new line in the string logged but no clue why all other new line chars later are not treated the same way.

---

<div class="post-metadata">

### Author: ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)
#### Post date: [25 March 2021 09:00 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/15 "2021-03-25T09:00:01Z")

</div>

Not really. In case of global variables there's no parent context like for flow.get("$parent.whatever").

---

<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: [25 March 2021 09:01 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/16 "2021-03-25T09:01:32Z")

</div>

> [@r0ller](#):
>
> I don't know why it appears as if there were more node.warns() either.

Did you try this ↓

> [@Steve-Mcl](#):
>
> Can you try `node.warn(["farmName", global.get("farmName")])`

What did you get?

---

<div class="post-metadata">

### Author: ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)
#### Post date: [25 March 2021 09:24 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/17 "2021-03-25T09:24:44Z")

</div>

Forgot it but now gave it a try via an inject node and I got:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/086da726ef65cb1fc16c0756ae682f252ba58ee8.png)

I also added it to the subflow but it takes time till it gets triggered.

---

<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: [25 March 2021 09:31 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/18 "2021-03-25T09:31:22Z")

</div>

Can you post the full code from this snipped (as text)?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/b/7b60cb4aa8aba62e320cf9f37eac37946a7d7491.png)

PS does this ↑ run in a subflow?

---

<div class="post-metadata">

### Author: ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)
#### Post date: [25 March 2021 09:36 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/19 "2021-03-25T09:36:46Z")

</div>

Sure. That's a function node I placed before the email node to filter and log the garbage and also to avoid that the email recipients are spammed with nonsense. This (and the related flow of course) run in a subflow.

```auto
if(msg.topic.startsWith("undefined")===true){
    node.warn("Phantom farm:"+global.get('farmName')+"\nPhantom payload:"+JSON.stringify(msg.payload)+"\nPhantom event:"+JSON.stringify(msg.event));
    node.warn(["farmName", global.get("farmName")]);
    return null;
}
else return msg;

```

---

<div class="post-metadata">

### Author: ![r0ller](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/r0ller/32/14327_2.png) [@r0ller](https://discourse.nodered.org/u/r0ller)
#### Post date: [25 March 2021 09:47 UTC](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132/20 "2021-03-25T09:47:37Z")

</div>

I had an idea so I created a new subflow, simply added a function node which reads the global variable and connected it to a debug node. When I trigger this subflow from outside with an inject node, the variable is read correctly so it does not seem to be a general issue of reading a global variable in a subflow. I'll investigate it further but nodered seems to be fine. However, if anyone has any idea on how to catch this red handed, it's welcome 🙂

[Next page](https://discourse.nodered.org/t/global-variable-undefined-in-subflow/43132.md?page=2)
