# Global context being affected by array function .replace

**URL:** <https://discourse.nodered.org/t/global-context-being-affected-by-array-function-replace/39014>\
**Category:** General\
**Created:** [12 January 2021 17:52 UTC](https://discourse.nodered.org/t/global-context-being-affected-by-array-function-replace/39014 "2021-01-12T17:52:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![maximus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maximus/32/66678_2.png) [@maximus](https://discourse.nodered.org/u/maximus)\
**Post date:** [12 January 2021 17:52 UTC](https://discourse.nodered.org/t/global-context-being-affected-by-array-function-replace/39014/1 "2021-01-12T17:52:13Z")

</div>

Hello all,

I am having an issue in a function node. I have stored previously the array `[" 10"," 11"," 12"," 13"," 14"," 15"]` in the global variable "destinations".

The code in the function is as follows:

```auto
var array = global.get("destinations");
for(i=0; i < array.length; i++){ // Go through all the array
    array[i] = array[i].replace(/ /g, ''); //Remove all the leading spaces that were already there (/ /g searches in all the string)
    }
msg.payload = array;
return msg;

```

The issue that I have is that when the instruction `array[i] = array[i].replace(/ /g, '');` gets executed, for some reason it stores the new value in the global context "destinations" though I never execute any instruction like context.set.

How is that possible? Could it be a bug? Thanks!

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [12 January 2021 18:12 UTC](https://discourse.nodered.org/t/global-context-being-affected-by-array-function-replace/39014/2 "2021-01-12T18:12:31Z")

</div>

This is because you are handling a pointer to the context variable and not a copy so this will effect the origin.  
You will need to clone the variable to manipulate it without doing it.  
Fortunately Nodered has a deep cloning function build into to the function nodes by default. Its documented here:

[https://nodered.org/docs/api/modules/v/0.20.0/@node-red\_util\_util.html#.cloneMessage](https://nodered.org/docs/api/modules/v/0.20.0/@node-red_util_util.html#.cloneMessage)  
Edit:  
Forgot an example.  
It can be accessed in a function node by using something like this:

```auto
var origin = global.get("origin");
var copy = RED.util.cloneMessage(origin);

```

---

<div class="post-metadata">

**Author:** ![maximus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maximus/32/66678_2.png) [@maximus](https://discourse.nodered.org/u/maximus)\
**Post date:** [12 January 2021 18:22 UTC](https://discourse.nodered.org/t/global-context-being-affected-by-array-function-replace/39014/3 "2021-01-12T18:22:25Z")

</div>

Ahh that is why!

If I understood correctly then, instead of `var array = global.get("destinations");` it should be `var array = cloneMessage(global.get("destinations"));`?

Does this also would mean that `array = "hello"` would be the same as `global.set("destinations", "hello")` in this case?

Thank you very much!

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [12 January 2021 18:33 UTC](https://discourse.nodered.org/t/global-context-being-affected-by-array-function-replace/39014/4 "2021-01-12T18:33:41Z")

</div>

> [@maximus](#):
>
> `var array = cloneMessage(global.get("destinations"));` ?

I edited the post above with an example of the correct syntax. I think nodered utilizes [lodash](https://lodash.com/) behind the scenes to do this:

> [@JGKK](#):
>
> ```auto
> var origin = global.get("origin");
> var copy = RED.util.cloneMessage(origin);
> 
> ```

To answer your second question:

> [@maximus](#):
>
> Does this also would mean that `array = "hello"` would be the same as `global.set("destinations", "hello")` in this case?

Effectively yes although I think it’s good practice to call the set function anyway but I don’t thinks it’s strictly always necessary.  
But not using the set method can of course also give you problems. For example if the global variable is not yet initialized and you do something like this:

```auto
var hello = global.get("hello") || "hello";
hello = "hello_test";

```

**hello** will get initialized to the string and you can change it within the function node but it will not be pointing at or initialize in global context so you would have to use set in this case.  
So to prevent edge cases like this it’s better to use set anyway.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [12 January 2021 19:27 UTC](https://discourse.nodered.org/t/global-context-being-affected-by-array-function-replace/39014/5 "2021-01-12T19:27:10Z")

</div>

> [@JGKK](#):
>
> Effectively yes although I think it’s good practice to call the set function anyway but I don’t thinks it’s strictly always necessary.

Actually, no - in this particular case, that is not how it would work.

If you start with:

```auto
var array = global.get("destinations");

```

then, as has been said, `array` is a pointer to the object held in context.

If you do an operation on that object, it modifies `array` _and_ the object in context.

However, if you do:

```auto
array = "hello"

```

then you are not changing the object in context - you are changing what _thing_ the `array` variable points at. So doing that type of reassignment wouldn't do anything to the object in context.

---

<div class="post-metadata">

**Author:** ![maximus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maximus/32/66678_2.png) [@maximus](https://discourse.nodered.org/u/maximus)\
**Post date:** [13 January 2021 11:52 UTC](https://discourse.nodered.org/t/global-context-being-affected-by-array-function-replace/39014/6 "2021-01-13T11:52:49Z")

</div>

Also mentioned here for additional reference:

> **[Node-Red Message and Object Cloning](http://www.steves-internet-guide.com/message-object-cloning/)**
>
> Why clone messages? Why Clone Objects? Message and object cloning needs to be don in certain circumstances and understanding this is important in flow development.

---

<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:** [27 January 2021 11:53 UTC](https://discourse.nodered.org/t/global-context-being-affected-by-array-function-replace/39014/7 "2021-01-27T11:53:00Z")

</div>

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