# Problem with array persistence with global.get / global.set

**URL:** <https://discourse.nodered.org/t/problem-with-array-persistence-with-global-get-global-set/34788>\
**Category:** General\
**Created:** [25 October 2020 18:23 UTC](https://discourse.nodered.org/t/problem-with-array-persistence-with-global-get-global-set/34788 "2020-10-25T18:23:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![HiFiJ](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@HiFiJ](https://discourse.nodered.org/u/HiFiJ)\
**Post date:** [25 October 2020 18:23 UTC](https://discourse.nodered.org/t/problem-with-array-persistence-with-global-get-global-set/34788/1 "2020-10-25T18:23:59Z")

</div>

Hi everyone,  
first timer here, very sorry to bother you. Have been reading and trying to get around on my own for hours now, but I really can not solve/implement how to store an array with global.set and read it back in next time my function node is called. I have searched and read dozens of forum threads and started to learn about reference vs copy (I understand that global.get gives me a reference to the array), but nevertheless I am unable to solve how to store and retrieve an array of objects.  
This is just my last variation, which has gives me 0 objects everytime once called:

```auto
var ind_data={};
ind_data.nodeid=msg.nodeId;
ind_data.type=msg.type;
ind_data.payload=msg.payload;

var stored_individual_data=global.get("global_invidual_data")||[];
msg.global_individual_data_orig=stored_individual_data.length;

msg.ind_data=ind_data;

stored_individual_data.push(ind_data);

global.set("global_individual_data",stored_individual_data);
msg.global_individual_data_post=stored_individual_data.length;

return msg;

```

Anyone can help me out?  
Thanks very much,

Joost

```auto
10/25/2020, 7:22:44 PMnode: 97bf2754.81864
Node 130 Voltage : msg : Object
object
payload: "3.46"
port: "/dev/ttyUSB0"
_msgid: "dc734303.014eb"
nodeId: 130
childSensorId: 9
messageType: 1
ack: 0
subType: 24
origin: 1
messageTypeStr: "C_SET"
subTypeStr: "V_VAR1"
topic: "Node 130 Voltage"
type: "volt"
timestampJC: "19:22"
nodeId_array: array[5]
global_individual_data_orig: 0
ind_data: object
global_individual_data_post: 2

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 October 2020 18:38 UTC](https://discourse.nodered.org/t/problem-with-array-persistence-with-global-get-global-set/34788/2 "2020-10-25T18:38:37Z")

</div>

> [@HiFiJ](#):
>
> `var stored_individual_data=global.get("global_invidual_data")||[];`

> [@HiFiJ](#):
>
> `global.set("global_individual_data",stored_individual_data);`

Spot the diliberate spelling mistake

`global_invidual_data`  
`global_individual_data`

---

<div class="post-metadata">

**Author:** ![HiFiJ](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@HiFiJ](https://discourse.nodered.org/u/HiFiJ)\
**Post date:** [25 October 2020 18:40 UTC](https://discourse.nodered.org/t/problem-with-array-persistence-with-global-get-global-set/34788/3 "2020-10-25T18:40:54Z")

</div>

Omg omg omg omg omg omg omg  
I am working on this problem for hours now. This can't be true. omg omg omg

Off to try things out just now, will get back.

(screaming)

---

<div class="post-metadata">

**Author:** ![HiFiJ](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@HiFiJ](https://discourse.nodered.org/u/HiFiJ)\
**Post date:** [25 October 2020 18:43 UTC](https://discourse.nodered.org/t/problem-with-array-persistence-with-global-get-global-set/34788/4 "2020-10-25T18:43:03Z")

</div>

Not sure if I'm happy it's working now (it is)or devastated I spent so much time on this...

Thank you very much for thinking/reading this through, supposedly I'd have never spotted that!

Joost

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [25 October 2020 18:45 UTC](https://discourse.nodered.org/t/problem-with-array-persistence-with-global-get-global-set/34788/5 "2020-10-25T18:45:06Z")

</div>

Gotta love this forum... ! Thanks @Steve-Mcl

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 October 2020 18:45 UTC](https://discourse.nodered.org/t/problem-with-array-persistence-with-global-get-global-set/34788/6 "2020-10-25T18:45:11Z")

</div>

> [@HiFiJ](#):
>
> Not sure if I'm happy it's working now (it is)or devastated

lol

If it makes you feel any better, it took me nearly 10 seconds to spot it. 😃 😛

its easy to get bogged down in the detail.

---

<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:** [25 October 2020 18:47 UTC](https://discourse.nodered.org/t/problem-with-array-persistence-with-global-get-global-set/34788/7 "2020-10-25T18:47:38Z")

</div>

The moral of the story, always check for typo's

---

<div class="post-metadata">

**Author:** ![HiFiJ](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@HiFiJ](https://discourse.nodered.org/u/HiFiJ)\
**Post date:** [25 October 2020 18:50 UTC](https://discourse.nodered.org/t/problem-with-array-persistence-with-global-get-global-set/34788/8 "2020-10-25T18:50:15Z")

</div>

The moral of the story always check for _ **typo's** _

😄 Ok will do

Thanks everyone, have a great day/night

---

<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:** [25 October 2020 18:58 UTC](https://discourse.nodered.org/t/problem-with-array-persistence-with-global-get-global-set/34788/9 "2020-10-25T18:58:15Z")

</div>

> [@HiFiJ](#):
>
> always check for _ **typo's** _

You are improving, see only 3 minutes this time.

---

<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:** [25 October 2020 22:05 UTC](https://discourse.nodered.org/t/problem-with-array-persistence-with-global-get-global-set/34788/10 "2020-10-25T22:05:07Z")

</div>

> [@HiFiJ](#):
>
> I am working on this problem for hours now.

We have all been there with typos. The brain knows what you meant to type so doesn't even bother reading it.  
Of course one moral might be not to use complex variable names that take a lot of typing, and in particular words like individual which I think are prone to typos. However it is also important not to go the other extreme and use three letter meaningless names.  
I will always remember taking over some code when I worked on a system monitoring car systems. The code had a variable `todo` and I wondered for ages why it was named that. It wasn't till I came to look back at it much later that I realised it was total odometer!

---

<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:** [8 November 2020 22:05 UTC](https://discourse.nodered.org/t/problem-with-array-persistence-with-global-get-global-set/34788/11 "2020-11-08T22:05:31Z")

</div>

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