Bug or what is wrong?

Hello, i start become crazy.
I have maked a big script in node red using the function node, now i deleted all just to understand what is wrong.
Please help.

Just a timestamp and a function node with this code .
I read 3 objects from flow or global (there is no difference), i write only the first object and all 3 get the same value.

Here is the output.

What is wrong ?
thank you

var tmp = {};

flow.set("simpos20",tmp);
flow.set("simpos50",tmp);
flow.set("simpos90",tmp);

var simpos20 = flow.get("simpos20");
var simpos50 = flow.get("simpos50");
var simpos90 = flow.get("simpos90");

node.warn(simpos20);
node.warn(simpos50);
node.warn(simpos90);

simpos20[1] = {"text":"hello"};

node.warn(simpos20);
node.warn(simpos50);
node.warn(simpos90);

return msg;
[
    {
        "id": "03084b6a8123d65d",
        "type": "function",
        "z": "4895129379e58dc6",
        "name": "",
        "func": "var tmp = {};\n\nflow.set(\"simpos20\",tmp);\nflow.set(\"simpos50\",tmp);\nflow.set(\"simpos90\",tmp);\n\nvar simpos20 = flow.get(\"simpos20\");\nvar simpos50 = flow.get(\"simpos50\");\nvar simpos90 = flow.get(\"simpos90\");\n\nnode.warn(simpos20);\nnode.warn(simpos50);\nnode.warn(simpos90);\n\n\nsimpos20[1] = {\"text\":\"hello\"};\n\nnode.warn(simpos20);\nnode.warn(simpos50);\nnode.warn(simpos90);\n\n\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 380,
        "y": 240,
        "wires": [
            []
        ]
    },
    {
        "id": "eaafbd92ddb9c638",
        "type": "inject",
        "z": "4895129379e58dc6",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 210,
        "y": 240,
        "wires": [
            [
                "03084b6a8123d65d"
            ]
        ]
    }
]

You are setting all three context values to the same object - tmp. So when you update that one object, all three references to it see the change.

It would be better to initialise them as different objects:

flow.set("simpos20",{});
flow.set("simpos50",{});
flow.set("simpos90",{});

Wow thank you.
I was sure to get 3 new objects when use flow.get . So i get 3 pointer of the object ?

To clarify that a bit, javascript handles objects by reference. So tmp exists as an actual object in memory, but when you say something like
let x = tmp
or
flow.set("simpos20",tmp)
you are effectively setting x to be a pointer to the object, and setting the context contents to be a pointer to the object. If you then change the contents of tmp the variable x and the context variables are pointing the now changed object, so they all appear to change.

Thank you very much !!!

Node red provides a utility function that can clone the msg/object and 'unlink' it from being assigned by reference.

RED.util.cloneMessage(tmp);

Test Flow:

[{"id":"03084b6a8123d65d","type":"function","z":"5847b7aa62131d37","name":"","func":"var tmp = {};\n\nflow.set(\"simpos20\",RED.util.cloneMessage(tmp));\nflow.set(\"simpos50\",RED.util.cloneMessage(tmp));\nflow.set(\"simpos90\",RED.util.cloneMessage(tmp));\n\nvar simpos20 = flow.get(\"simpos20\");\nvar simpos50 = flow.get(\"simpos50\");\nvar simpos90 = flow.get(\"simpos90\");\n\nnode.warn(simpos20);\nnode.warn(simpos50);\nnode.warn(simpos90);\n\n\nsimpos20[1] = {\"text\":\"hello\"};\n\nnode.warn(simpos20);\nnode.warn(simpos50);\nnode.warn(simpos90);\n\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":1580,"wires":[[]]},{"id":"eaafbd92ddb9c638","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":470,"y":1580,"wires":[["03084b6a8123d65d"]]}]

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