(This isn't saying you are wrong.)
This is my TEST flow.....
As broken down and as bare to the bones as I can get for testing.
FOREIGN NODES
random
and
counter
[{"id":"143135ba0109bdce","type":"function","z":"68e3655f.b606b4","name":"function 42","func":"let rainamount = context.get('rainamount') || [] // get context variable, default empty array\nif (msg.count == 0) { // if msg.count is 0, clear array (no idea if this is what you want)\n node.warn(\"Wiping existing data\");\n rainamount = [];\n return;\n}\n\n//let c = parseInt(msg.count);\nlet v = msg.payload;\n\nnode.warn(\"v = \" + v);\n\nrainamount.push(v) // Add a new array element \ncontext.set('rainamount', rainamount) // save to node context\n\n\n\nmsg.payload = context.get('rainammount');\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":910,"y":420,"wires":[["6930f7c042cd0dc7"]]},{"id":"a91a087025472415","type":"counter","z":"68e3655f.b606b4","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":750,"y":420,"wires":[["143135ba0109bdce","648218dcc8b28c55"]]},{"id":"6930f7c042cd0dc7","type":"debug","z":"68e3655f.b606b4","name":"debug 2491","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1090,"y":420,"wires":[]},{"id":"98764bfae77b82d1","type":"random","z":"68e3655f.b606b4","name":"","low":"1","high":"10","inte":"true","property":"payload","x":600,"y":420,"wires":[["a91a087025472415"]]},{"id":"a1b220d69af20028","type":"inject","z":"68e3655f.b606b4","name":"Reset","props":[{"p":"reset","v":"reset","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":610,"y":460,"wires":[["a91a087025472415"]]},{"id":"648218dcc8b28c55","type":"debug","z":"68e3655f.b606b4","name":"count","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"count","targetType":"msg","statusVal":"count","statusType":"auto","x":890,"y":460,"wires":[]},{"id":"16a399fdec8cb4c3","type":"inject","z":"68e3655f.b606b4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":460,"y":420,"wires":[["98764bfae77b82d1"]]}]
Main part of the code in the function
node.
let rainamount = context.get('rainamount') || [] // get context variable, default empty array
if (msg.count == 0) { // if msg.count is 0, clear array (no idea if this is what you want)
node.warn("Wiping existing data");
rainamount = [];
return;
}
//let c = parseInt(msg.count);
let v = msg.payload;
node.warn("v = " + v);
rainamount.push(v) // Add a new array element
context.set('rainamount', rainamount) // save to node context
msg.payload = context.get('rainammount');
return msg;
Which is basically what you posted with some slight name changes to accommodate the names in this flow.
I press the reset
node and you can see it wipes the values.
(Well, it kind of does, but as you can next see, the array is not wiped.)
(Not your fault. Just mentioning.)
Then when I send a message in, it is received and you see it with the node.warn()
line. v = 8
, but it isn't being stored because you get an undefined
in the debug
node.
EDIT
I think I know what is wrong with it not being wiped.
I am not wiping the context
part of rainammount
. My mistake.
(Not now I'm stuck how to wipe it in context.)
context.set(
rainammount) = [];
doesn't work.
(ok, just saw that mistake. Changed from to '. Still doesn't work.) FIXED that part
context.set('rainammount',); works. But am still / now getting
[empty]` when I inject from the other node.
ARGH!
Sticky m
key.