# Help with context variables and their treatment

**URL:** <https://discourse.nodered.org/t/help-with-context-variables-and-their-treatment/78351>\
**Category:** General\
**Created:** [11 May 2023 12:43 UTC](https://discourse.nodered.org/t/help-with-context-variables-and-their-treatment/78351 "2023-05-11T12:43:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RAD](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@RAD](https://discourse.nodered.org/u/RAD)\
**Post date:** [11 May 2023 12:43 UTC](https://discourse.nodered.org/t/help-with-context-variables-and-their-treatment/78351/1 "2023-05-11T12:43:43Z")

</div>

Hello everyone, sorry for my English, I only speak Spanish.  
I am new to Node-red, and I have come across this doubt.  
I want to save a data in a context variable, to later recover it without modifying its saved value.  
example:  
In a function block I store a value obtained from a process:

```auto
flow.set("variable.data",100);code here

```

but if later in another block I retrieve this value and do:

```auto
msg.payload = flow.get("variable");
msg.payload.data = 200;
return msg;

```

the value stored in flow.variable.data (context) is also modified (it becomes 200)  
I'm not understanding, shouldn't it be kept?  
Is it possible that an object cannot be saved to a context variable or do I need to do it as JSON?  
Sorry if it's a very stupid question.  
Greetings to everybody and thanks.

---

<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:** [11 May 2023 13:21 UTC](https://discourse.nodered.org/t/help-with-context-variables-and-their-treatment/78351/2 "2023-05-11T13:21:11Z")

</div>

Javascript stores arrays and objects by reference (google it you will get a full explanation).

So you say msg.payload = flow.get, now both context and payload point to the same memory reference.

You would need to copy/clone the value. Node-red provides RED.util.cloneMessage() for this reason  
[https://nodered.org/docs/api/modules/v/0.20.0/@node-red\_util\_util.html](https://nodered.org/docs/api/modules/v/0.20.0/@node-red_util_util.html)  
e.g

```auto
msg.payload = RED.util.cloneMessage( flow.get("variable") );

```

---

<div class="post-metadata">

**Author:** ![RAD](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@RAD](https://discourse.nodered.org/u/RAD)\
**Post date:** [13 May 2023 12:05 UTC](https://discourse.nodered.org/t/help-with-context-variables-and-their-treatment/78351/3 "2023-05-13T12:05:00Z")

</div>

Hello E1cid, thank you very much for your prompt response.  
Now it's clear to me what's going on, I missed that detail...  
If I apply the change node before the function block, does it get a "copy" of the context variable or am I wrong?  
slds

---

<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:** [13 May 2023 12:08 UTC](https://discourse.nodered.org/t/help-with-context-variables-and-their-treatment/78351/4 "2023-05-13T12:08:21Z")

</div>

The change node has an option to make a deep copy of a msg or context, as you can get the same issue when setting msg/context to another in some cases.

---

<div class="post-metadata">

**Author:** ![RAD](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@RAD](https://discourse.nodered.org/u/RAD)\
**Post date:** [13 May 2023 13:11 UTC](https://discourse.nodered.org/t/help-with-context-variables-and-their-treatment/78351/5 "2023-05-13T13:11:31Z")

</div>

Ok, that's more than clear to me.  
Once again, thank you very much for your help!  
slds  
Richard

---

<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 May 2023 13:12 UTC](https://discourse.nodered.org/t/help-with-context-variables-and-their-treatment/78351/6 "2023-05-27T13:12:03Z")

</div>

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