# Using Sub-Flow Env.Variable to set context failes

**URL:** <https://discourse.nodered.org/t/using-sub-flow-env-variable-to-set-context-failes/35991>\
**Category:** General\
**Created:** [16 November 2020 09:13 UTC](https://discourse.nodered.org/t/using-sub-flow-env-variable-to-set-context-failes/35991 "2020-11-16T09:13:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![crackytsi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crackytsi/32/28369_2.png) [@crackytsi](https://discourse.nodered.org/u/crackytsi)\
**Post date:** [16 November 2020 09:13 UTC](https://discourse.nodered.org/t/using-sub-flow-env-variable-to-set-context-failes/35991/1 "2020-11-16T09:13:10Z")

</div>

Hi,  
I have a sub-flow that has an environment Variable, lets call it "FILENAME".  
This variable will be set whenever the new sub-flow is used.  
Inside the sub-flow, there is a function node with the following code:

```auto
filn = env.get("FILENAME");
// filn has a value like "test.txt"
ctx = flow.get(filn);
if (ctx == undefined){
   // The context does not exist, we set it
   flow.set(filn, 1)
   msg.payload = 1;
} else {
   msg.payload = 0;
}

```

Additionally I have a configuration where cache for context file-storage is disabled.  
When I now deploy this configuration it failes with the following error:

> "Error: File Store cache disabled - only asynchronous access supported"

If I do the same outside a Sub-Flow it works without any issue.

Can you help me to understand the reason or an option to get this functionality implemented?

Thanks a lot 🙂

---

<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:** [16 November 2020 09:25 UTC](https://discourse.nodered.org/t/using-sub-flow-env-variable-to-set-context-failes/35991/2 "2020-11-16T09:25:32Z")

</div>

If you have disabled the cache for the file-store, then the store can only be accessed asynchronously.

> **[Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#asynchronous-context-access)**

```auto
filn = env.get("FILENAME");
// filn has a value like "test.txt"
flow.get(filn, function(err, ctx) {
  if (ctx == undefined){
     // The context does not exist, we set it
     flow.set(filn, 1, function(err) {
       msg.payload = 1;
       node.send(msg);
     });
  } else {
     msg.payload = 0;
     node.send(msg);
  }
});

```

---

<div class="post-metadata">

**Author:** ![crackytsi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crackytsi/32/28369_2.png) [@crackytsi](https://discourse.nodered.org/u/crackytsi)\
**Post date:** [16 November 2020 10:57 UTC](https://discourse.nodered.org/t/using-sub-flow-env-variable-to-set-context-failes/35991/3 "2020-11-16T10:57:11Z")

</div>

Thank you Nick 🙂  
You helped me a lot. I don't know why I didn't notice of this section. 🤔

---

<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:** [30 November 2020 10:57 UTC](https://discourse.nodered.org/t/using-sub-flow-env-variable-to-set-context-failes/35991/4 "2020-11-30T10:57:15Z")

</div>

Closed on 30 November 2020 10:57 UTC

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