# Help with storing temporary data in either an array or object

**URL:** <https://discourse.nodered.org/t/help-with-storing-temporary-data-in-either-an-array-or-object/87086>\
**Category:** General\
**Created:** [8 April 2024 21:42 UTC](https://discourse.nodered.org/t/help-with-storing-temporary-data-in-either-an-array-or-object/87086 "2024-04-08T21:42:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 April 2024 21:42 UTC](https://discourse.nodered.org/t/help-with-storing-temporary-data-in-either-an-array-or-object/87086/1 "2024-04-08T21:42:02Z")

</div>

(I am getting worse each passing day at remembering things)

I've tried to look for ways to do this but it is not fun. Confusion reigns supreme.

I have an incoming message.

eg:

`msg.rain = 0, msg.count = 0`  
Then next time I get  
`msg.rain = 0, msg.count = 1`

I want to _store_ these in an..... _array_? or object. It isn't really critical which is the best as it is only for inspection and the data is only needed for maybe 12 iterations then wiped.  
(Eventually this won't even be needed. This is just a passing curiosity.)

So I think I want to do something like:

```auto
rain[msg.count] = msg.rain;

```

each time so then I can look at the node's context and see what is happening.

But my attempts to understand how to do it are not really giving me anything I can understand.  
Would someone mind helping me with getting my head around this - please.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 April 2024 22:25 UTC](https://discourse.nodered.org/t/help-with-storing-temporary-data-in-either-an-array-or-object/87086/2 "2024-04-08T22:25:25Z")

</div>

What is msg.count counting? Is it just a counter 0, 1, 2, ... 11?

What does msg.rain mean - is it a measurement of rain quantity or a boolean 0/1?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 April 2024 22:27 UTC](https://discourse.nodered.org/t/help-with-storing-temporary-data-in-either-an-array-or-object/87086/3 "2024-04-08T22:27:21Z")

</div>

`msg.count` is - as you said/asked - just a series of `1`, `2`, `3`, `4`, etc.  
And _reset_ if `0`.

`msg.rain` is the amount of rain. So it is a number/measurement.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 April 2024 22:36 UTC](https://discourse.nodered.org/t/help-with-storing-temporary-data-in-either-an-array-or-object/87086/4 "2024-04-08T22:36:45Z")

</div>

So I guess you need an array context variable  
something like

```auto
let rainamount = context.get('rainamount') || [] // get context variable, default empty array
if (msg.count == 0) { // if msg.count is 0, clear array (no idea if this is what you want)
   rainamount = []
}
rainamount.push(msg.rain) // Add a new array element 
context.set('rainamount', rainamount) // save to node context

```

eg [0, 0, 0, 2, 1, 4, 0, 0, 0, 0, 0, 0]

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 April 2024 22:43 UTC](https://discourse.nodered.org/t/help-with-storing-temporary-data-in-either-an-array-or-object/87086/5 "2024-04-08T22:43:07Z")

</div>

That really looks like what I want to do.

But on/in my searches I found/saw stuff like that it it didn't work.

(Nothing against you in me saying that)

I am also torn between oversimplifying what I am doing and posting a whole lot of stuff I couldn't expect anyone to understand.

What I have **SEEMS TO WORK** - I've seen it doing the right thing.  
But - me being ADHD/OCD/_What ever_ wanted to drill down on what was/IS going on and became obsessed with seeing things that weren't there.  
Sorry, WANTING to see things that weren't there.

And so I am now digging this huge hole and am floundering in what is going on.

I'll give it a go and let you know.

Alas that can't be until tomorrow (or later on today) as it is 08:41 and ...... I need it to go past 12:00 to check / see what happens.

Yes, it doesn't really matter one way or the other if I let things _really happen_ with what data I have now.  
But again it loops back to the ADHD / OCD problem and it isn't fun.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 April 2024 22:49 UTC](https://discourse.nodered.org/t/help-with-storing-temporary-data-in-either-an-array-or-object/87086/6 "2024-04-08T22:49:58Z")

</div>

> [@Trying\_to\_learn](#):
>
> But on/in my searches I found/saw stuff like that it it didn't work.  
> (Nothing against you in me saying that)

Well I have not tested that code, it might not work for the particular flow you are building.

Let's hope it does, if not, I apologise in advance for my bad advice. 😀

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 April 2024 22:51 UTC](https://discourse.nodered.org/t/help-with-storing-temporary-data-in-either-an-array-or-object/87086/7 "2024-04-08T22:51:49Z")

</div>

I appreciate the names may not be right.

I am testing it with/on the broken down test flow I made with the needed name changes.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 April 2024 23:07 UTC](https://discourse.nodered.org/t/help-with-storing-temporary-data-in-either-an-array-or-object/87086/8 "2024-04-08T23:07:48Z")

</div>

(This isn't saying you are wrong.)

This is my TEST flow.....  
As broken down and as bare to the bones as I can get for testing.

FOREIGN NODES  
`random`  
and  
`counter`

```auto
[{"id":"143135ba0109bdce","type":"function","z":"68e3655f.b606b4","name":"function 42","func":"let rainamount = context.get('rainamount') || [] // get context variable, default empty array\nif (msg.count == 0) { // if msg.count is 0, clear array (no idea if this is what you want)\n node.warn(\"Wiping existing data\");\n rainamount = [];\n return;\n}\n\n//let c = parseInt(msg.count);\nlet v = msg.payload;\n\nnode.warn(\"v = \" + v);\n\nrainamount.push(v) // Add a new array element \ncontext.set('rainamount', rainamount) // save to node context\n\n\n\nmsg.payload = context.get('rainammount');\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":910,"y":420,"wires":[["6930f7c042cd0dc7"]]},{"id":"a91a087025472415","type":"counter","z":"68e3655f.b606b4","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":750,"y":420,"wires":[["143135ba0109bdce","648218dcc8b28c55"]]},{"id":"6930f7c042cd0dc7","type":"debug","z":"68e3655f.b606b4","name":"debug 2491","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1090,"y":420,"wires":[]},{"id":"98764bfae77b82d1","type":"random","z":"68e3655f.b606b4","name":"","low":"1","high":"10","inte":"true","property":"payload","x":600,"y":420,"wires":[["a91a087025472415"]]},{"id":"a1b220d69af20028","type":"inject","z":"68e3655f.b606b4","name":"Reset","props":[{"p":"reset","v":"reset","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":610,"y":460,"wires":[["a91a087025472415"]]},{"id":"648218dcc8b28c55","type":"debug","z":"68e3655f.b606b4","name":"count","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"count","targetType":"msg","statusVal":"count","statusType":"auto","x":890,"y":460,"wires":[]},{"id":"16a399fdec8cb4c3","type":"inject","z":"68e3655f.b606b4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":460,"y":420,"wires":[["98764bfae77b82d1"]]}]

```

 ![Screenshot from 2024-04-09 09-05-33](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/ca154baf00428a85cec0065f9f8829834f293b32.png)

Main part of the code in the `function` node.

```auto
let rainamount = context.get('rainamount') || [] // get context variable, default empty array
if (msg.count == 0) { // if msg.count is 0, clear array (no idea if this is what you want)
    node.warn("Wiping existing data");
    rainamount = [];
    return;
}

//let c = parseInt(msg.count);
let v = msg.payload;

node.warn("v = " + v);

rainamount.push(v) // Add a new array element 
context.set('rainamount', rainamount) // save to node context

msg.payload = context.get('rainammount');
return msg;

```

Which is basically what you posted with some slight name changes to accommodate the names in this flow.

I press the `reset` node and you can see it _wipes_ the values.  
(Well, it kind of does, but as you can next see, the array is not wiped.)  
(Not your fault. Just mentioning.)

Then when I send a message in, it is received and you see it with the `node.warn()` line. `v = 8`, but it isn't being stored because you get an `undefined` in the `debug` node.

## EDIT

I think I know what is wrong with it not being wiped.

I am not wiping the `context` part of `rainammount`. My mistake.  
(Not now I'm stuck how to wipe it in context.)  
`context.set(`rainammount`) = [];` doesn't work.  
(ok, just saw that mistake. Changed from `to '. Still doesn't work.) FIXED that part` context.set('rainammount',);` works. But am still / now getting`[empty]` when I inject from the other node.

# ARGH!

Sticky `m` key.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 April 2024 23:35 UTC](https://discourse.nodered.org/t/help-with-storing-temporary-data-in-either-an-array-or-object/87086/9 "2024-04-08T23:35:16Z")

</div>

> [@Trying\_to\_learn](#):
>
> ```auto
> msg.payload = context.get('rainammount');
> return msg;
> 
> ```

Yes, you have rainammount (two ms) there.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 April 2024 23:37 UTC](https://discourse.nodered.org/t/help-with-storing-temporary-data-in-either-an-array-or-object/87086/10 "2024-04-08T23:37:10Z")

</div>

## Yeah, sorry.... My fault.

## Apologies.

Not sure what is going on there. But it seems to work - in the test flow at least.

I'll migrate it to the REAL one this afternoon/night I hope.

---

<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:** [22 April 2024 23:37 UTC](https://discourse.nodered.org/t/help-with-storing-temporary-data-in-either-an-array-or-object/87086/11 "2024-04-22T23:37:49Z")

</div>

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