# Unwanted circular update of variables

**URL:** <https://discourse.nodered.org/t/unwanted-circular-update-of-variables/71126>\
**Category:** General\
**Created:** [23 November 2022 16:05 UTC](https://discourse.nodered.org/t/unwanted-circular-update-of-variables/71126 "2022-11-23T16:05:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Switchblade2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/switchblade2/32/60655_2.png) [@Switchblade2](https://discourse.nodered.org/u/Switchblade2)\
**Post date:** [23 November 2022 16:05 UTC](https://discourse.nodered.org/t/unwanted-circular-update-of-variables/71126/1 "2022-11-23T16:05:39Z")

</div>

I'm creating a new array from a global array and for a reason I cannot understand the global array gets updated on the same time as the new one, see example.

For info, I'm using ContextStorage to 'memory' as default and optionally to 'file', the circular issue occurs for both types of storage.

```auto
[
    {
        "id": "6dd670b22423b177",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "c18f55a1a1dc87e4",
        "type": "inject",
        "z": "6dd670b22423b177",
        "name": "add 15 min",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "00 17 * * *",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 130,
        "y": 220,
        "wires": [
            [
                "73e3f0e04dd115c8"
            ]
        ]
    },
    {
        "id": "73e3f0e04dd115c8",
        "type": "function",
        "z": "6dd670b22423b177",
        "name": "+15 min",
        "func": "var dd = global.get(\"ccc\");\ndd.minute = dd.minute + 15\n\nif (dd.minute > 59) {\n dd.minute = dd.minute - 60\n dd.hour = dd.hour + 1\n}\n\nflow.set(\"ddd\", dd, \"file\");\n\nreturn null;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 280,
        "y": 220,
        "wires": [
            []
        ]
    },
    {
        "id": "68da3984b80c8313",
        "type": "inject",
        "z": "6dd670b22423b177",
        "name": "set time",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "30 04 * * *",
        "once": false,
        "onceDelay": "0.5",
        "topic": "",
        "payload": "true",
        "payloadType": "bool",
        "x": 120,
        "y": 180,
        "wires": [
            [
                "d57ef3b9af0306be"
            ]
        ]
    },
    {
        "id": "d57ef3b9af0306be",
        "type": "function",
        "z": "6dd670b22423b177",
        "name": "Save times",
        "func": "var cc = { minute: 0, hour: 19 };\nglobal.set(\"ccc\", cc);",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 290,
        "y": 180,
        "wires": [
            []
        ]
    }
]

```

---

<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:** [23 November 2022 16:18 UTC](https://discourse.nodered.org/t/unwanted-circular-update-of-variables/71126/2 "2022-11-23T16:18:16Z")

</div>

This comes up regularly, most recently a few hours ago. A lot can be learned by watching the forum and trying to at least read all the questions, and following any threads that look interesting.

> [@Issues with variables](https://discourse.nodered.org/t/issues-with-variables/71121):
>
> Hello everyone, I'm having some problem with basic variables manipulations. Here's a code example that does the issue. let CopyOfOriginal = context.get('CopyOfOriginal') || []; let OriginalObject = { payload:'Im original' } CopyOfOriginal.push(OriginalObject); context.set('CopyOfOriginal', CopyOfOriginal); // Modify after the context.set OriginalObject.payload = 'Im modified'; // Push without context.set CopyOfOriginal.push(OriginalObject); return msg; I'm expecting the CopyOfOrigi…

---

<div class="post-metadata">

**Author:** ![Switchblade2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/switchblade2/32/60655_2.png) [@Switchblade2](https://discourse.nodered.org/u/Switchblade2)\
**Post date:** [23 November 2022 16:32 UTC](https://discourse.nodered.org/t/unwanted-circular-update-of-variables/71126/3 "2022-11-23T16:32:09Z")

</div>

Ok, thanks. I did a search but probably not with the good search words (beginners error to define the problem)

---

<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:** [23 November 2022 16:42 UTC](https://discourse.nodered.org/t/unwanted-circular-update-of-variables/71126/4 "2022-11-23T16:42:40Z")

</div>

It is often difficult to search for related issues when you are not really sure what the issue is 🙂

---

<div class="post-metadata">

**Author:** ![Switchblade2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/switchblade2/32/60655_2.png) [@Switchblade2](https://discourse.nodered.org/u/Switchblade2)\
**Post date:** [23 November 2022 16:58 UTC](https://discourse.nodered.org/t/unwanted-circular-update-of-variables/71126/5 "2022-11-23T16:58:27Z")

</div>

RED.util.cloneMessage() worked like a charm. Thanks

---

<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:** [7 December 2022 16:59 UTC](https://discourse.nodered.org/t/unwanted-circular-update-of-variables/71126/6 "2022-12-07T16:59:00Z")

</div>

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