# Context variables

**URL:** <https://discourse.nodered.org/t/context-variables/21951>\
**Category:** General\
**Created:** [19 February 2020 00:48 UTC](https://discourse.nodered.org/t/context-variables/21951 "2020-02-19T00:48:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abskki](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@abskki](https://discourse.nodered.org/u/abskki)\
**Post date:** [19 February 2020 00:48 UTC](https://discourse.nodered.org/t/context-variables/21951/1 "2020-02-19T00:48:13Z")

</div>

Hi, guys

Just come across something strange. Could anyone give me some guides? Thanks heaps your time in advance.

Problem: I set two flow variables. When I assign value to one of the variables, another one get the same value assigned.

 ![Screen Shot 2020-02-19 at 1.34.05 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/e/4e56a6294e7c90184a45a9b7c7dda3208ff5edbc.png) This text will be hidden

I use function: change data format to create flow variable: dataFromFirebaseFormated. Then I use dataFromFirebaseFormated to create another flow variable: sensorData. When I use function: store data to store data to variable: sensorData, the same value is stored at variable: dataFromFirebaseFormated. Show as annotation below

 ![Screen Shot 2020-02-19 at 1.43.37 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b5dddc2472f505e7e77465752fb0094906f738b1.png)

function: change data format

```auto
// data of card-allocation from firebase
let dataFromFirebase = JSON.parse(msg.payload);

let dataFromFirebaseFormated = flow.get('dataFromFirebaseFormated') || [];

// 1. take each value from variable dataFromFirebase
for (let x in dataFromFirebase) {
    dataFromFirebaseFormated.push(dataFromFirebase[x]);
}

// 2. change format of mac from 'F09AC0BBA5E8' to ['F0', '9A', 'C0', 'BB', 'A5', 'E8']
dataFromFirebaseFormated.forEach(function(value) {
    value.mac = value.mac.match(/.{1,2}/g);
});

// 3. change format of mac from ['F0', '9A', 'C0', 'BB', 'A5', 'E8'] to [240, 154, 192, 187, 165, 232]
dataFromFirebaseFormated.forEach(function(value) {
    value.mac.forEach(function(value2, index) {
        value.mac[index] = parseInt(value2, 16);
        value.anchor = [];
        value.presence = [];
        value.panic = 0;
        value.date = 0;
    });
});

flow.set('dataFromFirebaseFormated', dataFromFirebaseFormated);

msg.payload = dataFromFirebaseFormated;

return msg;

```

function: store data

```auto
// data of card-allocation from firebase
let dataFromFirebase = JSON.parse(msg.payload);

let dataFromFirebaseFormated = flow.get('dataFromFirebaseFormated') || [];

// 1. take each value from variable dataFromFirebase
for (let x in dataFromFirebase) {
    dataFromFirebaseFormated.push(dataFromFirebase[x]);
}

// 2. change format of mac from 'F09AC0BBA5E8' to ['F0', '9A', 'C0', 'BB', 'A5', 'E8']
dataFromFirebaseFormated.forEach(function(value) {
    value.mac = value.mac.match(/.{1,2}/g);
});

// 3. change format of mac from ['F0', '9A', 'C0', 'BB', 'A5', 'E8'] to [240, 154, 192, 187, 165, 232]
dataFromFirebaseFormated.forEach(function(value) {
    value.mac.forEach(function(value2, index) {
        value.mac[index] = parseInt(value2, 16);
        value.anchor = [];
        value.presence = [];
        value.panic = 0;
        value.date = 0;
    });
});

flow.set('dataFromFirebaseFormated', dataFromFirebaseFormated);

msg.payload = dataFromFirebaseFormated;

return msg;

```

node info:  
18 Feb 23:21:20 - [info] Node-RED version: v1.0.3  
18 Feb 23:21:20 - [info] Node.js version: v10.19.0

---

<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:** [19 February 2020 14:27 UTC](https://discourse.nodered.org/t/context-variables/21951/2 "2020-02-19T14:27:17Z")

</div>

I haven't had time to look in detail at your code but I imagine this is caused by the fact that in javascript objects are referred to by reference rather than by value. So if you save an object in the context you are actually just saving a pointer to that object not the object itself. Then if you save the same object in another variable in context it will save another pointer to the same object. So if you then change the object it will appear to have changed in both places. The solution is to make a copy of the object before saving it. You can do that using `RED.util.cloneMessage(..)`. So maybe in store data you might do something like

```auto
let dataFromFirebaseFormated = RED.util.cloneMessage(flow.get('dataFromFirebaseFormated') || []);

```

Though having said that I am not certain that cloneMessage will work with an array, there may be a better way to make a copy of an array. Try it and see.

---

<div class="post-metadata">

**Author:** ![abskki](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@abskki](https://discourse.nodered.org/u/abskki)\
**Post date:** [19 February 2020 21:17 UTC](https://discourse.nodered.org/t/context-variables/21951/3 "2020-02-19T21:17:00Z")

</div>

The problem was indeed caused by reference mechanism.

Thanks so much!!!

---

<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:** [4 March 2020 21:17 UTC](https://discourse.nodered.org/t/context-variables/21951/4 "2020-03-04T21:17:01Z")

</div>

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