# Initial global variable not working debug undefined

**URL:** <https://discourse.nodered.org/t/initial-global-variable-not-working-debug-undefined/44886>\
**Category:** General\
**Created:** [28 April 2021 17:58 UTC](https://discourse.nodered.org/t/initial-global-variable-not-working-debug-undefined/44886 "2021-04-28T17:58:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![riker65](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/riker65/32/38453_2.png) [@riker65](https://discourse.nodered.org/u/riker65)\
**Post date:** [28 April 2021 17:58 UTC](https://discourse.nodered.org/t/initial-global-variable-not-working-debug-undefined/44886/1 "2021-04-28T17:58:32Z")

</div>

Hi

I am having a change node to set a global variable  
triggery by time inject node

wondering why after deployment variable is not defined.

checking with debug node

what am I missing?

```auto
[{"id":"510cbd9c.4b2f24","type":"inject","z":"322adaad.4e52d6","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":70,"y":140,"wires":[["19d20c76.08dc54","9985e5a6.4fa788"]]},{"id":"9985e5a6.4fa788","type":"change","z":"322adaad.4e52d6","name":"Parameter","rules":[{"t":"set","p":"test_global","pt":"global","to":"init","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":540,"wires":[["2ebf8083.e9338"]]},{"id":"2ebf8083.e9338","type":"debug","z":"322adaad.4e52d6","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"global.test_global","targetType":"msg","statusVal":"payload.soc_toggle_low","statusType":"auto","x":570,"y":540,"wires":[]}]

```

Thanks t

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [28 April 2021 18:20 UTC](https://discourse.nodered.org/t/initial-global-variable-not-working-debug-undefined/44886/2 "2021-04-28T18:20:48Z")

</div>

[https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)  
What kind of context storage option you are using? Persistent context memory is not default option, it must be configured to take effect.

---

<div class="post-metadata">

**Author:** ![riker65](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/riker65/32/38453_2.png) [@riker65](https://discourse.nodered.org/u/riker65)\
**Post date:** [28 April 2021 18:27 UTC](https://discourse.nodered.org/t/initial-global-variable-not-working-debug-undefined/44886/3 "2021-04-28T18:27:00Z")

</div>

Hi Thanks,

did not change the default setting yet.

but I assumed that having the inject after deployment the global variable should be set via change node ?

I also used some functions with global.get() , which was not working

have to check the context options

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [28 April 2021 18:37 UTC](https://discourse.nodered.org/t/initial-global-variable-not-working-debug-undefined/44886/4 "2021-04-28T18:37:19Z")

</div>

There is nothing wrong with the way which you have set the Global context, the error is arising from the debug node containing `global.test_global` as its output.  
You cannot obtain the Global context like that, you need to retrieve it first using a change or function node, and then feed that into the debug.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [28 April 2021 18:48 UTC](https://discourse.nodered.org/t/initial-global-variable-not-working-debug-undefined/44886/5 "2021-04-28T18:48:16Z")

</div>

However, you could add a little jsonata to the debug node to output your Global context. try;

```auto
[{"id":"9985e5a6.4fa788","type":"change","z":"b3b413d1.05b1b","name":"Parameter","rules":[{"t":"set","p":"test_global","pt":"global","to":"init","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":1530,"wires":[["2ebf8083.e9338"]]},{"id":"2ebf8083.e9338","type":"debug","z":"b3b413d1.05b1b","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"$globalContext('test_global')","targetType":"jsonata","statusVal":"payload.soc_toggle_low","statusType":"auto","x":510,"y":1530,"wires":[]},{"id":"b5f8b3a7.fdd3c","type":"inject","z":"b3b413d1.05b1b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":145,"y":1530,"wires":[["9985e5a6.4fa788"]]}]

```

![context](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/1/a139e2933a1544526275c0e7de8241fd8207c232.jpeg)

---

<div class="post-metadata">

**Author:** ![riker65](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/riker65/32/38453_2.png) [@riker65](https://discourse.nodered.org/u/riker65)\
**Post date:** [29 April 2021 04:49 UTC](https://discourse.nodered.org/t/initial-global-variable-not-working-debug-undefined/44886/6 "2021-04-29T04:49:18Z")

</div>

ok understood, will try with a function node

---

<div class="post-metadata">

**Author:** ![riker65](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/riker65/32/38453_2.png) [@riker65](https://discourse.nodered.org/u/riker65)\
**Post date:** [29 April 2021 04:49 UTC](https://discourse.nodered.org/t/initial-global-variable-not-working-debug-undefined/44886/7 "2021-04-29T04:49:31Z")

</div>

Perfect, thanks, this is working

---

<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:** [13 May 2021 04:50 UTC](https://discourse.nodered.org/t/initial-global-variable-not-working-debug-undefined/44886/8 "2021-05-13T04:50:17Z")

</div>

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