Why the object has double brackets after get back from a flow

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.



topic: i:0 name:test.  **<=== this is what I want.**



topic: i:0 name:undefined.   **<=== unexpectedly result.** 



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?

Can you post your full flow ?
I tried to replicate and used node.warn instead of console.log and it seems to work fine.

Thanks for reply. I have update the flow.

There is something strange going on, cannot put my finger on it, but in the 2nd function node; return cache - which should that be returned ? change it into return null and it works.

ha~ it works after return the null on 2nd function node. To return cache just for debug, it is fine to me to return the null.


msg.payload = cache;
return msg;

It works. Do you know the root cause?

The function must always return a msg object. Returning a number or string will result in an error.

I think I find the reason. You post and link give me a idea.
The cache that I return was an array not an object. The Node-RED document said the brackets needed if you want to return an array directly. So I made the change to use brackets on cache and it goes well.

console.log ("start");
cache = flow.get ("test");
console.log ("Cache:"+JSON.stringify(cache));

for (var i in cache)
    console.log ("topic:"+msg.topic+ "  i:"+ i +"  name:"+cache[i].name);

return [cache];

Thank you all the help.

I'm not sure whether this is correct. I think it should be an array of messages:

... If there is more than one output, an array of messages can be returned by the function to send to the outputs.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.