# Calling Environment variables as a Global variable name

**URL:** <https://discourse.nodered.org/t/calling-environment-variables-as-a-global-variable-name/81232>\
**Category:** General\
**Tags:** function-node\
**Created:** [12 September 2023 09:51 UTC](https://discourse.nodered.org/t/calling-environment-variables-as-a-global-variable-name/81232 "2023-09-12T09:51:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![douchebagraccoon](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@douchebagraccoon](https://discourse.nodered.org/u/douchebagraccoon)\
**Post date:** [12 September 2023 09:51 UTC](https://discourse.nodered.org/t/calling-environment-variables-as-a-global-variable-name/81232/1 "2023-09-12T09:51:57Z")

</div>

Hello everyone,

I'm attempting to do the same thing as in the topic linked below :

Continuing the discussion from [Calling Environment Variables in Change Node for Global Variable name](https://discourse.nodered.org/t/calling-environment-variables-in-change-node-for-global-variable-name/77747/5):

```auto
let envr = env.get("evr_name") ?? "0";
msg["ArrayDefect" + envr] = msg.payload;
return msg;

```

I tried to apply this logic to create a global variable whose name originates from an environment variable but can't get it to work ; and the person at the end didn't explain how they succeeded 🥲

I would highly appreciate some help !

Thanks in advance.

---

<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:** [12 September 2023 11:49 UTC](https://discourse.nodered.org/t/calling-environment-variables-as-a-global-variable-name/81232/2 "2023-09-12T11:49:41Z")

</div>

> [@douchebagraccoon](#):
>
> but can't get it to work

Your code looks fine. what do you see when this runs? Did you try @E1cid first suggestion using a change node?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/2/02fd97fd6e48263a8932839574c55dd3f4e05b3a.png)

Can you provide a minimal flow and instructions that demonstrates your issue?

---

<div class="post-metadata">

**Author:** ![douchebagraccoon](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@douchebagraccoon](https://discourse.nodered.org/u/douchebagraccoon)\
**Post date:** [12 September 2023 12:15 UTC](https://discourse.nodered.org/t/calling-environment-variables-as-a-global-variable-name/81232/3 "2023-09-12T12:15:20Z")

</div>

Thank you for your reply !

The global variable I wish to name isn't an array so using a change node wouldn't do the trick ; the provided code does function but only when setting a msg and not a global variable.

---

<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:** [12 September 2023 12:25 UTC](https://discourse.nodered.org/t/calling-environment-variables-as-a-global-variable-name/81232/4 "2023-09-12T12:25:23Z")

</div>

> [@douchebagraccoon](#):
>
> The global variable I wish to name isn't an array so using a change node wouldn't do the trick

No one said your env var was an array, nor does that change node consider the env var to be an array.

> [@douchebagraccoon](#):
>
> the provided code does function but only when setting a msg and not a global variable.

That would be because that snippet of code is not designed to set global variables (by which I guess you mean set "global context" variable?)

> [@douchebagraccoon](#):
>
> I tried to apply this logic to create a global variable whose name originates from an environment variable but can't get it to work ; and the person at the end didn't explain how they succeeded 🥲

That linked post was nothing to do with setting a global variable (whatever that is) so even if they did explain, it would not work for you.

* * *

One more try to help you.

### 1)

What are you actually trying to set and for what reason? What do you mean by "I tried to apply this logic to create a global variable"? Global Context? If yes, then a select `global` instead of `msg` in the change node!

### 2)

> [@Steve-Mcl](#):
>
> Can you provide a minimal flow and instructions that demonstrates your issue?

---

<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:** [12 September 2023 12:38 UTC](https://discourse.nodered.org/t/calling-environment-variables-as-a-global-variable-name/81232/5 "2023-09-12T12:38:57Z")

</div>

What I _suspect_ you are trying to do is

1. get a variable name from env var
2. store msg.payload in global context with the name retrieved in step 1

If I am correct, then something like this...

```js
const evrName = env.get("evr_name")
if (!evrName) {
  node.warn('evrName is empty!')
  return null
}
global.set(evrName, msg.payload)
return msg;

```

---

<div class="post-metadata">

**Author:** ![douchebagraccoon](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@douchebagraccoon](https://discourse.nodered.org/u/douchebagraccoon)\
**Post date:** [13 September 2023 07:30 UTC](https://discourse.nodered.org/t/calling-environment-variables-as-a-global-variable-name/81232/6 "2023-09-13T07:30:42Z")

</div>

Sorry for not explaining thoroughly, I thought the topic I linked explained my issue well enough...

And in the original topic, someone mentioned the change node could be used if the global context variable is an array and to otherwise use a function node, which is why I added that precision.

---

<div class="post-metadata">

**Author:** ![douchebagraccoon](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@douchebagraccoon](https://discourse.nodered.org/u/douchebagraccoon)\
**Post date:** [13 September 2023 07:31 UTC](https://discourse.nodered.org/t/calling-environment-variables-as-a-global-variable-name/81232/7 "2023-09-13T07:31:24Z")

</div>

That is **exactly** what I need.

Thank you ! And sorry again for not explaining my issue correctly.

For future reference, is there a way to do this with a change node ?  
I don't know what the correct syntax is to call on an environment variable into a global context name.

---

<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:** [13 September 2023 08:36 UTC](https://discourse.nodered.org/t/calling-environment-variables-as-a-global-variable-name/81232/8 "2023-09-13T08:36:47Z")

</div>

> [@douchebagraccoon](#):
>
> For future reference, is there a way to do this with a change node

Yes & No.

If you nest your global context variables in an object (which is always sensible as it makes managing lots of global variables easier) you CAN use a change node.

Example:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/6/8619489a27efa3dbec0bc145c5eea7022aac9d6a.png)

---

<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:** [27 September 2023 08:37 UTC](https://discourse.nodered.org/t/calling-environment-variables-as-a-global-variable-name/81232/9 "2023-09-27T08:37:11Z")

</div>

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