No data on second function output

var msg2 = [{payload:"",filename:""}];

msg2.filename = "test";
msg2.payload = "test2";

msg.payload = "test3";

return [msg,msg2];

Though I fill msg2.filename and msg2.payload with data, the second debug output shows only the empty strings from line 1!??

complete flow to test:

[
    {
        "id": "e82841526f31da38",
        "type": "function",
        "z": "ada67f26f66765ce",
        "name": "",
        "func": "var msg2 = [{payload:\"\",filename:\"\"}];\n\nmsg2.filename = \"test\";\nmsg2.payload = \"test2\";\n\nmsg.payload = \"test3\";\n\nreturn [msg,msg2];\n",
        "outputs": 2,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 420,
        "y": 200,
        "wires": [
            [
                "5240e61ec7f320bf"
            ],
            [
                "ae5614bcead71a36"
            ]
        ]
    },
    {
        "id": "875462fc390b6934",
        "type": "inject",
        "z": "ada67f26f66765ce",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 220,
        "y": 200,
        "wires": [
            [
                "e82841526f31da38"
            ]
        ]
    },
    {
        "id": "5240e61ec7f320bf",
        "type": "debug",
        "z": "ada67f26f66765ce",
        "name": "debug 35",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 620,
        "y": 160,
        "wires": []
    },
    {
        "id": "ae5614bcead71a36",
        "type": "debug",
        "z": "ada67f26f66765ce",
        "name": "debug 36",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 620,
        "y": 240,
        "wires": []
    }
]

No need an array for the second output.

var msg2 = {payload:"test2",filename:"test"};
msg.payload = "test3";

return [msg,msg2];
1 Like

The above demo is simple, but I want to change both of the values during the code, so I cannot set them at the beginning!?
But it is valid to change the values during the function code, isn't it?

You can change the values as many times as you desire. The only time it matters is when you return them.

I did this many times before. But then why does my debug 36 not return the values above??
BTW: I test it on Node-RED 4.0.2. My Smart-home Node-RED is still at 3.1.9, but I can reproduce the same effect there.

Have you taken the square brackets off? It should be
var msg2 = {payload:"",filename:""};

You should use let rather than var, but that is not what is causing the problem.

Thanks, @Colin ! That was the error!!

@GogoVega did point that out in his first post. It is that one you should mark as the solution.

1 Like

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