# Saving Array to Global

**URL:** <https://discourse.nodered.org/t/saving-array-to-global/28090>\
**Category:** General\
**Created:** [10 June 2020 01:51 UTC](https://discourse.nodered.org/t/saving-array-to-global/28090 "2020-06-10T01:51:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikeh](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@mikeh](https://discourse.nodered.org/u/mikeh)\
**Post date:** [10 June 2020 01:51 UTC](https://discourse.nodered.org/t/saving-array-to-global/28090/1 "2020-06-10T01:51:04Z")

</div>

I've saved an array to a global variable using global.set  
In another function, I used global.get to extract that array but only a few elements existed!  
Don't know what's happening at all...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/f/3f38359244070f1a60260d31c8926c5c9c3e88f8.png)

As you can see I saved it:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/acdb681be6eb396dae5a053820c2adbb9c265804.png)

Then extracted it:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6f7f323160d5ba3dca765bd3d95eb0f82578a2c8.png)

But instead of 16 it just returned 6 elements?

---

<div class="post-metadata">

**Author:** ![mikeh](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@mikeh](https://discourse.nodered.org/u/mikeh)\
**Post date:** [10 June 2020 02:19 UTC](https://discourse.nodered.org/t/saving-array-to-global/28090/2 "2020-06-10T02:19:33Z")

</div>

Somehow it gets affected by this node "Set Data"  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/b/eb66487a1995e58fca3da39e7aacc3675b816710.png)

So I changed it to:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/0/202bd5d4e95c036764b26fe156775704be5b7c19.png)

And the global variable worked fine. Weird!  
What's in the code that affected the global variable?

---

<div class="post-metadata">

**Author:** ![softy2k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/softy2k/32/23707_2.png) [@softy2k](https://discourse.nodered.org/u/softy2k)\
**Post date:** [10 June 2020 06:04 UTC](https://discourse.nodered.org/t/saving-array-to-global/28090/3 "2020-06-10T06:04:53Z")

</div>

Probably, between set and get, msg.payload (including pfex) was modified due to code execution. I assume that if you save array variables that contain objects, that variable behaves like a pointer.

You can try the following: you convert the array variable (object) to a string, and then at get you convert it to array. Just test.

`global.set('pfex', JSON.stringify(msg.payload));`

then:

`msg.payload = JSON.parse(global.get('pfex'));`

I repeat, it's just a guess, I'm a beginner too.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [10 June 2020 06:32 UTC](https://discourse.nodered.org/t/saving-array-to-global/28090/4 "2020-06-10T06:32:11Z")

</div>

In Javascript, objects are passed by reference. Arrays are just objects in JS, so the array you stored in the global context refers to the same one as the message payload.

Your "set data" function modifies that object downstream, specifically it sets `msg.payload.length = 6;` in line 6. That effectively **truncates** the array to 6 elements. See [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/Array/length](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length) for reference.

Using an array object like a normal object is considered bad practice. What you _should_ do, is creating a new object for your payload first, like `msg.payload = {}`, and set your properties after that.

---

<div class="post-metadata">

**Author:** ![mikeh](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@mikeh](https://discourse.nodered.org/u/mikeh)\
**Post date:** [10 June 2020 07:43 UTC](https://discourse.nodered.org/t/saving-array-to-global/28090/5 "2020-06-10T07:43:32Z")

</div>

@kuema You're absolutely correct! msg.payload.length is the culprit! Should be more careful in using this variable name next time!

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [10 June 2020 07:45 UTC](https://discourse.nodered.org/t/saving-array-to-global/28090/6 "2020-06-10T07:45:58Z")

</div>

You can use `length` nonetheless, as long as you use it on a newly created object, and not an array. Unless you explicitly want to truncate it.

---

<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:** [24 June 2020 07:45 UTC](https://discourse.nodered.org/t/saving-array-to-global/28090/7 "2020-06-24T07:45:59Z")

</div>

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