# Persistent global context used in template node

**URL:** https://discourse.nodered.org/t/persistent-global-context-used-in-template-node/83537
**Category:** General
**Created:** [8 December 2023 20:23 UTC](https://discourse.nodered.org/t/persistent-global-context-used-in-template-node/83537 "2023-12-08T20:23:35Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Tany](https://avatars.discourse-cdn.com/v4/letter/t/dfb087/32.png) [@Tany](https://discourse.nodered.org/u/Tany)
#### Post date: [8 December 2023 20:23 UTC](https://discourse.nodered.org/t/persistent-global-context-used-in-template-node/83537/1 "2023-12-08T20:23:35Z")

</div>

Hallo,

This code works perfect in a template node (html) to retrieve cvMode as a global variable... in memory.

Now I installed contrib-persistent-global-context, and with a function node... global.set('cvMode', "ON", "file").  
Set & Get work fine.

settings.js contextStorage is set:  
contextStorage: {  
default: "memoryOnly",  
memoryOnly: { module: 'memory' },  
file: { module: 'localfilesystem' }  
},

7 Dec 09:17:27 - [info] Node-RED version: v3.1.0  
7 Dec 09:17:27 - [info] Node.js version: v18.18.2  
7 Dec 09:17:27 - [info] Linux 6.1.0-rpi6-rpi-v8 arm64 LE

How to change value="{{global.cvMode}}" to get the variable from "file" ?

I also tried function getDocGVF which is called by windows.onload as part of :  
function getDocGVF() {  
document.getElementById('cvMode').value = global.get('cvMode', "file");  
}

Neither worked.  
☹

Pls your advise.  
Thanks.  
T

---

<div class="post-metadata">

### Author: ![Tany](https://avatars.discourse-cdn.com/v4/letter/t/dfb087/32.png) [@Tany](https://discourse.nodered.org/u/Tany)
#### Post date: [8 December 2023 20:26 UTC](https://discourse.nodered.org/t/persistent-global-context-used-in-template-node/83537/2 "2023-12-08T20:26:12Z")

</div>

oops... the copy lost the code:  
input type="text" id="cvMode" name="cvMode" value="{{global.cvMode}}"

---

<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: [8 December 2023 20:58 UTC](https://discourse.nodered.org/t/persistent-global-context-used-in-template-node/83537/3 "2023-12-08T20:58:28Z")

</div>

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [8 December 2023 21:23 UTC](https://discourse.nodered.org/t/persistent-global-context-used-in-template-node/83537/4 "2023-12-08T21:23:05Z")

</div>

> [@Tany](#):
>
> How to change value="{{global.cvMode}}" to get the variable from "file" ?

From template node help text

> t is possible to use a property from the flow context or global context. Just use `{{flow.name}}` or `{{global.name}}` , or for persistable store `store` use `{{flow[store].name}}` or `{{global[store].name}}` .

So `{{global[file].cvMode}}` should work

---

<div class="post-metadata">

### Author: ![Tany](https://avatars.discourse-cdn.com/v4/letter/t/dfb087/32.png) [@Tany](https://discourse.nodered.org/u/Tany)
#### Post date: [9 December 2023 08:24 UTC](https://discourse.nodered.org/t/persistent-global-context-used-in-template-node/83537/5 "2023-12-09T08:24:22Z")

</div>

Thank You.

1. I will comply the 3 backticks next time.  
2.Yes, the suggestion works perfect
2. the template node help text... slipped my attention. Too much focussed on the syntax of set/get.  
Best Regards.  
T

---

<div class="post-metadata">

### Author: ![Tany](https://avatars.discourse-cdn.com/v4/letter/t/dfb087/32.png) [@Tany](https://discourse.nodered.org/u/Tany)
#### Post date: [9 December 2023 12:05 UTC](https://discourse.nodered.org/t/persistent-global-context-used-in-template-node/83537/6 "2023-12-09T12:05:20Z")

</div>

All GlobalVariables work now fine in functions and can be used in html.  
On RPi I can observe them landing in directory /home/rpi/.node-red/context/global/global.json.

But when I drag an Inject, the Persistent-Global-Context and a Debug node on a flow, then I get :  
"Error: ENOENT: no such file or directory, open 'undefined/GlobalContext.json'"  
and  
"Cannot find json file inside [undefined/GlobalContext.json]!" from the Debug node

A Stop/Start of Node-Red does not show errors on the CommandLine of RPi.

What am I missing ?

Cheers,  
T

---

<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: [9 December 2023 13:19 UTC](https://discourse.nodered.org/t/persistent-global-context-used-in-template-node/83537/7 "2023-12-09T13:19:58Z")

</div>

> [@Tany](#):
>
> Persistent-Global-Context

I don't understand what you mean by that.

> [@Tany](#):
>
> I installed contrib-persistent-global-context,

There is no need for that, persistent context is built into node-red core.

---

<div class="post-metadata">

### Author: ![Tany](https://avatars.discourse-cdn.com/v4/letter/t/dfb087/32.png) [@Tany](https://discourse.nodered.org/u/Tany)
#### Post date: [9 December 2023 13:47 UTC](https://discourse.nodered.org/t/persistent-global-context-used-in-template-node/83537/8 "2023-12-09T13:47:22Z")

</div>

Thank You.  
As there is no need... I removed it, and all works still fine.  
As a NR novice I just tried to understand why

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

Cheers,

T

---

<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: [9 December 2023 14:06 UTC](https://discourse.nodered.org/t/persistent-global-context-used-in-template-node/83537/9 "2023-12-09T14:06:54Z")

</div>

> [@Tany](#):
>
> I just tried to understand why

I tried it and get the same error. It appears to be a bug in the node. You could report an issue on the node's github page if you wanted to.

---

<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: [7 February 2024 14:07 UTC](https://discourse.nodered.org/t/persistent-global-context-used-in-template-node/83537/10 "2024-02-07T14:07:35Z")

</div>

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