# Saving array to flow variable

**URL:** <https://discourse.nodered.org/t/saving-array-to-flow-variable/82893>\
**Category:** General\
**Created:** [15 November 2023 16:43 UTC](https://discourse.nodered.org/t/saving-array-to-flow-variable/82893 "2023-11-15T16:43:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marten-coaxial](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@marten-coaxial](https://discourse.nodered.org/u/marten-coaxial)\
**Post date:** [15 November 2023 16:43 UTC](https://discourse.nodered.org/t/saving-array-to-flow-variable/82893/1 "2023-11-15T16:43:20Z")

</div>

Hi,

I am having an issue to saving an array to a flow variable:

```auto
var l = flow.get("l") || []
l.push(msg.payload)
flow.set("l", l)
return l;

```

 ![Screenshot 2023-11-15 173824](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/b/3b09192ff04239d57fc2a0bc16535e6d0074b129.png)

From my understanding of the documentation and the forum. this should get the array from the flow and then append the array, however, it always warps each object in additional array. As seen here:

![Screenshot 2023-11-15 173944](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/08befa475510d65c6f5855575704f7975032e52b.png)

I want this as ['value', 'value']

What am I doing wrong?

`[{"id":"ac8ddef2a4f9cad8","type":"inject","z":"a8c551010c2bc0bc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"value","payloadType":"str","x":210,"y":660,"wires":[["156ea39e637c0279"]]},{"id":"156ea39e637c0279","type":"function","z":"a8c551010c2bc0bc","name":"function 2","func":"var l = flow.get(\"l\") || []\nl.push(msg.payload)\nflow.set(\"l\", l)\nreturn l;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":660,"wires":[["03ed314908d9848a"]]},{"id":"03ed314908d9848a","type":"debug","z":"a8c551010c2bc0bc","name":"debug 11","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":600,"y":660,"wires":[]}]`

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 November 2023 17:06 UTC](https://discourse.nodered.org/t/saving-array-to-flow-variable/82893/2 "2023-11-15T17:06:49Z")

</div>

You must always return a message object from a function node. Also nowadays you should be using let or const rather than var, thought that is not an issue here. Try this

```auto
let l = flow.get("l") || []
l.push(msg.payload)
flow.set("l", l)
msg.payload = l
return msg

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [15 November 2023 17:07 UTC](https://discourse.nodered.org/t/saving-array-to-flow-variable/82893/3 "2023-11-15T17:07:14Z")

</div>

As you are returning l which would produce  
"Function tried to send a message of type string"

Try `return {payload: l};`

Return l is causing a object referencing issue and creating the 1 element arrays in you context var.

---

<div class="post-metadata">

**Author:** ![marten-coaxial](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@marten-coaxial](https://discourse.nodered.org/u/marten-coaxial)\
**Post date:** [16 November 2023 17:41 UTC](https://discourse.nodered.org/t/saving-array-to-flow-variable/82893/4 "2023-11-16T17:41:51Z")

</div>

Thanks @Colin and @E1cid for pointing out that I need to return a message object. That was a head scratcher, maybe the error message here should be a bit more forthcoming... I really strugle to understand why returning `l` changes it but fixed is fixed for now 🙂

---

<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:** [15 January 2024 17:42 UTC](https://discourse.nodered.org/t/saving-array-to-flow-variable/82893/5 "2024-01-15T17:42:30Z")

</div>

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