# Store two var in context not working

**URL:** <https://discourse.nodered.org/t/store-two-var-in-context-not-working/2062>\
**Category:** General\
**Created:** [5 August 2018 09:36 UTC](https://discourse.nodered.org/t/store-two-var-in-context-not-working/2062 "2018-08-05T09:36:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsgwiro1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tsgwiro1/32/1565_2.png) [@tsgwiro1](https://discourse.nodered.org/u/tsgwiro1)\
**Post date:** [5 August 2018 09:36 UTC](https://discourse.nodered.org/t/store-two-var-in-context-not-working/2062/1 "2018-08-05T09:36:59Z")

</div>

Hi

I tried to store two variables in the context of a node. Unfortunately only one is stored and the other is not.

Here my function:  
\<\<\<  
// define output  
var outval = context.get('outval')||1;

// select valve  
var count = context.get('count')||0;  
count += 1;

if (count \> 3) {  
count = 0;  
if (outval \> 0)  
outval = 0;  
else  
outval = 1;  
}

if (count === 0) msg.topic = "inTopic1";  
if (count === 1) msg.topic = "inTopic2";  
if (count === 2) msg.topic = "inTopic3";  
if (count === 3) msg.topic = "inTopic4";

if (outval === 0)  
msg.payload = "0";  
else  
msg.payload = "1";

// store the values back  
context.set('count',count);  
context.set('outval',outval);

// make it part of the outgoing msg object  
msg.count = count;  
msg.outval = outval;

return msg;

> > >

The variable 'count' does correctly count from 0...3, the variable 'outval' is alway 1 after context.get (it is not recognised and always initialised with 1) but is correctly changed by the function --\> count=4 outval changes. But the change is not stored to the context, it does not know 'outval'???

I run v0.18.7

Help very much apprechiated

---

<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:** [5 August 2018 10:10 UTC](https://discourse.nodered.org/t/store-two-var-in-context-not-working/2062/2 "2018-08-05T10:10:29Z")

</div>

I don't see an obvious problem. Build a mini-flow with your function node and some inject/debug nodes that demonstrates the problem and export those nodes here so we can replicate exactly what you are doing.

---

<div class="post-metadata">

**Author:** ![tsgwiro1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tsgwiro1/32/1565_2.png) [@tsgwiro1](https://discourse.nodered.org/u/tsgwiro1)\
**Post date:** [5 August 2018 10:30 UTC](https://discourse.nodered.org/t/store-two-var-in-context-not-working/2062/3 "2018-08-05T10:30:05Z")

</div>

Here my example flow / nodes:

```auto
[{"id":"a9e7f9d2.fb50c","type":"debug","z":"e26efed.f6a578","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":610,"y":300,"wires":[]},{"id":"c681a2f1.789788","type":"inject","z":"e26efed.f6a578","name":"","topic":"","payload":"1","payloadType":"str","repeat":"2","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":300,"wires":[["2d3971b6.a64766"]]},{"id":"2d3971b6.a64766","type":"function","z":"e26efed.f6a578","name":"select valve","func":"// define output\nvar outval = context.get('outval')||1;\n\n// select valve\nvar count = context.get('count')||0;\ncount += 1;\n\nif (count > 3) {\n count = 0;\n if (outval > 0)\n outval = 0;\n else\n outval = 1;\n}\n\nif (count === 0) msg.topic = \"inTopic1\";\nif (count === 1) msg.topic = \"inTopic2\";\nif (count === 2) msg.topic = \"inTopic3\";\nif (count === 3) msg.topic = \"inTopic4\";\n\nif (outval === 0) \n msg.payload = \"0\";\nelse\n msg.payload = \"1\";\n\n// store the values back\ncontext.set('count',count);\ncontext.set('outval',outval);\n\n// make it part of the outgoing msg object\nmsg.count = count;\nmsg.outval = outval;\n\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":300,"wires":[["a9e7f9d2.fb50c"]]}]

```

---

<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:** [5 August 2018 10:53 UTC](https://discourse.nodered.org/t/store-two-var-in-context-not-working/2062/4 "2018-08-05T10:53:01Z")

</div>

> [@tsgwiro1](#):
>
> var outval = context.get('outval')||1;

The problem is this line. The `||` technique of getting a default value works by giving you the right hand value if the left hand value is 'false' like. In JavaScript, the value 0 considered false like, so it will always replace a value of 0 with 1.

If 0 is a valid value, you need to use the more explicit check for undefined to set the default:

```auto
var outval = context.get('outval');
if (outval === undefined) {
   outval = 1;
}

```

---

<div class="post-metadata">

**Author:** ![tsgwiro1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tsgwiro1/32/1565_2.png) [@tsgwiro1](https://discourse.nodered.org/u/tsgwiro1)\
**Post date:** [5 August 2018 11:13 UTC](https://discourse.nodered.org/t/store-two-var-in-context-not-working/2062/6 "2018-08-05T11:13:54Z")

</div>

Great! That was the problem.  
I'm not familliar with javascript and thought this was some sort of special operator in case a variable is undefined… something new learned 🙂
