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