Store two var in context not working


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;
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";
msg.payload = "1";

// store the values back

// 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

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.

Here my example flow / nodes:

[{"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"]]}]

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:

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

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 :slight_smile: