Hi
I am currently develop a flow. The idea is a function node can aggregate multiple msg.payload in a pre-define JSON format. But, I am facing a strange problem, not sure why. Could you help me on this?
Below is my code block
Function node #1
var profile = [{"name":"test"}];
flow.set ("ProfileCache", profile);
return null;
Function node #2
console.log ("start");
cache = flow.get ("ProfileCache");
console.log ("Cache:"+JSON.stringify(cache));
for (var i in cache)
{
console.log ("topic:"+msg.topic+ " i:"+ i +" name:"+cache[i].name);
}
return cache;
I run the Function node#1 first to set [{"name":"test"}]
into flow w/ key "ProfileCache". Then I trigger the Function node#2 to show the result. Below is the result when I trigger it twice and more.
start
Cache:[{"name":"test"}]
topic: i:0 name:test. **<=== this is what I want.**
start
Cache:[[{"name":"test","_msgid":"2f0f5de3.271b42"}]]
topic: i:0 name:undefined. **<=== unexpectedly result.**
start
Cache:[[{"name":"test","_msgid":"48506c12.3f3cd4"}]]
topic: i:0 name:undefined. **<=== unexpectedly result.**
flow on Node-Red version 0.19.0
[{"id":"c31d5fa6.553228","type":"function","z":"722f3d50.0bfb8c","name":"ProfileCache","func":"console.log (\"start\");\ncache = flow.get (\"ProfileCache\");\nconsole.log (\"Cache:\"+JSON.stringify(cache));\n\n// for (let i = 0 ; i < cache.length ; i++)\nfor (var i in cache)\n{\n console.log (\"topic:\"+msg.topic+ \" i:\"+ i +\" name:\"+cache[i].name);\n}\n\nreturn cache;","outputs":1,"noerr":0,"x":1450,"y":340,"wires":[["735c94ef.b86424"]]},{"id":"735c94ef.b86424","type":"debug","z":"722f3d50.0bfb8c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1630,"y":340,"wires":[]},{"id":"7d418003.8d84e8","type":"inject","z":"722f3d50.0bfb8c","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1230,"y":340,"wires":[["c31d5fa6.553228"]]},{"id":"9370fa50.41e8b8","type":"inject","z":"722f3d50.0bfb8c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":"0.01","x":1230,"y":260,"wires":[["ed3d4143.0efc"]]},{"id":"ed3d4143.0efc","type":"function","z":"722f3d50.0bfb8c","name":"Profile","func":"var profile = [{\"name\":\"test\"}];\nflow.set (\"ProfileCache\", profile);\n\nreturn null;","outputs":1,"noerr":0,"x":1410,"y":260,"wires":[[]]}]
Then I aware the name result is undefined except first time. Not sure why a double brackets there.
Does any one know why?