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

**URL:** <https://discourse.nodered.org/t/why-the-object-has-double-brackets-after-get-back-from-a-flow/38067>\
**Category:** General\
**Created:** [27 December 2020 04:44 UTC](https://discourse.nodered.org/t/why-the-object-has-double-brackets-after-get-back-from-a-flow/38067 "2020-12-27T04:44:27Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![chc69](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chc69/32/34289_2.png) [@chc69](https://discourse.nodered.org/u/chc69)\
**Post date:** [27 December 2020 04:44 UTC](https://discourse.nodered.org/t/why-the-object-has-double-brackets-after-get-back-from-a-flow/38067/1 "2020-12-27T04:44:27Z")

</div>

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

```auto
var profile = [{"name":"test"}];
flow.set ("ProfileCache", profile);
return null;

```

Function node #2

```auto
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.

```auto
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**

```auto
[{"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?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [27 December 2020 06:48 UTC](https://discourse.nodered.org/t/why-the-object-has-double-brackets-after-get-back-from-a-flow/38067/2 "2020-12-27T06:48:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chc69](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chc69/32/34289_2.png) [@chc69](https://discourse.nodered.org/u/chc69)\
**Post date:** [27 December 2020 07:09 UTC](https://discourse.nodered.org/t/why-the-object-has-double-brackets-after-get-back-from-a-flow/38067/3 "2020-12-27T07:09:45Z")

</div>

Thanks for reply. I have update the flow.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [27 December 2020 07:16 UTC](https://discourse.nodered.org/t/why-the-object-has-double-brackets-after-get-back-from-a-flow/38067/4 "2020-12-27T07:16:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chc69](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chc69/32/34289_2.png) [@chc69](https://discourse.nodered.org/u/chc69)\
**Post date:** [27 December 2020 07:35 UTC](https://discourse.nodered.org/t/why-the-object-has-double-brackets-after-get-back-from-a-flow/38067/5 "2020-12-27T07:35:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [27 December 2020 10:08 UTC](https://discourse.nodered.org/t/why-the-object-has-double-brackets-after-get-back-from-a-flow/38067/6 "2020-12-27T10:08:54Z")

</div>

Try:

```auto
msg.payload = cache;
return msg;

```

---

<div class="post-metadata">

**Author:** ![chc69](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chc69/32/34289_2.png) [@chc69](https://discourse.nodered.org/u/chc69)\
**Post date:** [27 December 2020 11:26 UTC](https://discourse.nodered.org/t/why-the-object-has-double-brackets-after-get-back-from-a-flow/38067/7 "2020-12-27T11:26:52Z")

</div>

It works. Do you know the root cause?

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [27 December 2020 11:33 UTC](https://discourse.nodered.org/t/why-the-object-has-double-brackets-after-get-back-from-a-flow/38067/8 "2020-12-27T11:33:38Z")

</div>

> [@chc69](#):
>
> Do you know the root cause?

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

[https://nodered.org/docs/user-guide/writing-functions](https://nodered.org/docs/user-guide/writing-functions)

---

<div class="post-metadata">

**Author:** ![chc69](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chc69/32/34289_2.png) [@chc69](https://discourse.nodered.org/u/chc69)\
**Post date:** [28 December 2020 15:57 UTC](https://discourse.nodered.org/t/why-the-object-has-double-brackets-after-get-back-from-a-flow/38067/9 "2020-12-28T15:57:26Z")

</div>

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.

```auto
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.  
cheers

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [28 December 2020 17:10 UTC](https://discourse.nodered.org/t/why-the-object-has-double-brackets-after-get-back-from-a-flow/38067/10 "2020-12-28T17:10:37Z")

</div>

> [@chc69](#):
>
> The Node-RED document said the brackets needed if you want to return an array directly.

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [11 January 2021 17:10 UTC](https://discourse.nodered.org/t/why-the-object-has-double-brackets-after-get-back-from-a-flow/38067/11 "2021-01-11T17:10:41Z")

</div>

Closed on 11 January 2021 17:10 UTC

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