# How to access a context variable in a switch node?

**URL:** <https://discourse.nodered.org/t/how-to-access-a-context-variable-in-a-switch-node/25426>\
**Category:** General\
**Created:** [24 April 2020 14:20 UTC](https://discourse.nodered.org/t/how-to-access-a-context-variable-in-a-switch-node/25426 "2020-04-24T14:20:13Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![DamianosS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/damianoss/32/16990_2.png) [@DamianosS](https://discourse.nodered.org/u/DamianosS)\
**Post date:** [24 April 2020 14:20 UTC](https://discourse.nodered.org/t/how-to-access-a-context-variable-in-a-switch-node/25426/1 "2020-04-24T14:20:13Z")

</div>

Hi everyone! Maybe I can't see the forest for the trees, but I thought I ask.  
In a `switch node`, how I can access a flow's context variable? In the dropdown `flow.` and `global.` are available, but not `context.`.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/c/1c7eba3b4d5bcd5c10b5eb08048f753690e86b5b.png)

Thanks,  
Damos

---

<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 April 2020 14:21 UTC](https://discourse.nodered.org/t/how-to-access-a-context-variable-in-a-switch-node/25426/2 "2020-04-24T14:21:49Z")

</div>

context is specific to a node (usually a function node)

as you are looking at a switch node, context is meaningless

unless I'm missing your point?

---

<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:** [24 April 2020 14:26 UTC](https://discourse.nodered.org/t/how-to-access-a-context-variable-in-a-switch-node/25426/3 "2020-04-24T14:26:17Z")

</div>

> [@DamianosS](#):
>
> how I can access a flow's context variable?

Use the `flow.` option - that is Flow context.

If you wanted to use Global context, you'd use the `global.` option.

---

<div class="post-metadata">

**Author:** ![DamianosS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/damianoss/32/16990_2.png) [@DamianosS](https://discourse.nodered.org/u/DamianosS)\
**Post date:** [24 April 2020 14:30 UTC](https://discourse.nodered.org/t/how-to-access-a-context-variable-in-a-switch-node/25426/4 "2020-04-24T14:30:47Z")

</div>

If I'd use a function-node, it'd be a no-brainer:

```auto
myVar = context.get("theVariable");
if (myVar == "something") {
...
}

```

But, I'd like to use a switch node  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/0/60ff7e316379b5388ff3af9fe5d14d9cfa208838.png)  
The scenario above would be possible if my context variable would be `flow` or `global`:

```auto
myVar = flow.get("theVariable");
if (myVar == "something") {
...
}

```

can be replaced with  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/4/243221ea6b0c77283e8cd5691ac0d83f0172bbad.png)  
but not with a context 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:** [24 April 2020 14:34 UTC](https://discourse.nodered.org/t/how-to-access-a-context-variable-in-a-switch-node/25426/5 "2020-04-24T14:34:22Z")

</div>

Sorry, not getting how you could use this?

Imagine the switch node did have context - how would you set it for later usage in a comparison?

Or do you mean using the switch to access an other nodes context?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [24 April 2020 14:37 UTC](https://discourse.nodered.org/t/how-to-access-a-context-variable-in-a-switch-node/25426/6 "2020-04-24T14:37:14Z")

</div>

The one small part of it's own context a switch has is previous value  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/ab4120cb974705f07120184ac0e870f0ba1831f0.png)

---

<div class="post-metadata">

**Author:** ![DamianosS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/damianoss/32/16990_2.png) [@DamianosS](https://discourse.nodered.org/u/DamianosS)\
**Post date:** [24 April 2020 14:40 UTC](https://discourse.nodered.org/t/how-to-access-a-context-variable-in-a-switch-node/25426/7 "2020-04-24T14:40:21Z")

</div>

> [@knolleary](#):
>
> Use the `flow.` option - that is Flow context.
> 
> If you wanted to use Global context, you'd use the `global.` option.

Sure, @knolleary. But, what about the third kind of variables? I understood that

- `context.xyz is visible to the node` (which people also call `flow`)
- `flow.xyz is visible to the flow` (which people also call a `tab`)
- `global.xyz is visible to everything`

I'd like to use context.xyz in a switch-node.

---

<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:** [24 April 2020 14:42 UTC](https://discourse.nodered.org/t/how-to-access-a-context-variable-in-a-switch-node/25426/8 "2020-04-24T14:42:07Z")

</div>

> [@DamianosS](#):
>
> `context.xyz is visible to the node` (which people also call `flow` )

No. `context.xyz` is only visible to the individual node. It is only exposed in the Function node. That is not called `flow`.

`flow.xyz` is the Flow context - visible to everything on the same tab.

---

<div class="post-metadata">

**Author:** ![DamianosS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/damianoss/32/16990_2.png) [@DamianosS](https://discourse.nodered.org/u/DamianosS)\
**Post date:** [24 April 2020 14:52 UTC](https://discourse.nodered.org/t/how-to-access-a-context-variable-in-a-switch-node/25426/9 "2020-04-24T14:52:33Z")

</div>

Ok, got it.

So, a variable is either visible to a particular node (as `context.xyz` in a `function` node) OR to the whole tab (as `flow.xyz`). There's no context to a particular flow (a series of connected nodes), correct?

If correct, why not?

---

<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:** [24 April 2020 14:56 UTC](https://discourse.nodered.org/t/how-to-access-a-context-variable-in-a-switch-node/25426/10 "2020-04-24T14:56:47Z")

</div>

Correct - there is no context that is scoped to just a set of connected nodes.

Why? Because there isn't. It just isn't something that exists.

There are a number of technical reasons that makes it harder to do than you might think. A group of connected nodes has no identity in the way a tab does. If you have two disconnected groups of nodes and then you wire them together, what becomes of their individual contexts? Do they merge? If so, how does the runtime identify them? Likewise, if you have a flow which you then split into two - who gets the context?

---

<div class="post-metadata">

**Author:** ![DamianosS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/damianoss/32/16990_2.png) [@DamianosS](https://discourse.nodered.org/u/DamianosS)\
**Post date:** [24 April 2020 15:00 UTC](https://discourse.nodered.org/t/how-to-access-a-context-variable-in-a-switch-node/25426/11 "2020-04-24T15:00:48Z")

</div>

> [@knolleary](#):
>
> Why? Because there isn't. It just isn't something that exists.

🙂

Thanks for the explanation, Nick. Yes, you're absolutely right. There would be a ton of consecutive problems.

Thanks to everyone!

---

<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 April 2020 15:06 UTC](https://discourse.nodered.org/t/how-to-access-a-context-variable-in-a-switch-node/25426/12 "2020-04-24T15:06:49Z")

</div>

> [@DamianosS](#):
>
> There's no context to a particular flow (a series of connected nodes), correct?

If you want a value to be available along a series of nodes then put the data in the message and pass it on.

---

<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:** [8 May 2020 15:06 UTC](https://discourse.nodered.org/t/how-to-access-a-context-variable-in-a-switch-node/25426/13 "2020-05-08T15:06:59Z")

</div>

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