Node-RED发送多个消息只返回第一个消息

Node-RED Sending multiple msg only returning first msg

我正在尝试使用 NodeRed 发送多条消息。

我尝试了两种选择:尝试使用一个输出发送,并尝试使用两个输出发送。

Processing1 代码:

var msg1 = {};
var msg2 = {};

msg1.topic = "fooTopic1";
msg2.topic = "barTopic1";

msg1.payload = "fooLoad1";
msg2.payload = "barLoad1";

return [msg1,msg2];

结果:

Processing2 代码:

var msg1 = {};
var msg2 = {};

msg1.topic = "fooTopic2";
msg2.topic = "barTopic2";

msg1.payload = "fooLoad2";
msg2.payload = "barLoad2";

return msg1,msg2;

我期待 2 条单独的消息,但调试只输出一条。 我做错了什么?

想要自测的人的流程:

[
    {
        "id": "5824c01b.a28ab",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": ""
    },
    {
        "id": "4902c067.f3c27",
        "type": "inject",
        "z": "5824c01b.a28ab",
        "name": "Input",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 150,
        "y": 220,
        "wires": [
            [
                "6a361af6.b5edf4"
            ]
        ]
    },
    {
        "id": "6a361af6.b5edf4",
        "type": "function",
        "z": "5824c01b.a28ab",
        "name": "Processing2",
        "func": "var msg1 = {};\nvar msg2 = {};\n\nmsg1.topic = \"fooTopic2\";\nmsg2.topic = \"barTopic2\";\n\nmsg1.payload = \"fooLoad2\";\nmsg2.payload = \"barLoad2\";\n\nreturn msg1,msg2;",
        "outputs": 2,
        "noerr": 0,
        "x": 350,
        "y": 220,
        "wires": [
            [
                "727ebca3.2270a4"
            ],
            [
                "727ebca3.2270a4"
            ]
        ]
    },
    {
        "id": "727ebca3.2270a4",
        "type": "debug",
        "z": "5824c01b.a28ab",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 530,
        "y": 220,
        "wires": []
    },
    {
        "id": "6b1d928e.2752bc",
        "type": "inject",
        "z": "5824c01b.a28ab",
        "name": "Input",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 150,
        "y": 160,
        "wires": [
            [
                "cc02ad73.9febe"
            ]
        ]
    },
    {
        "id": "cc02ad73.9febe",
        "type": "function",
        "z": "5824c01b.a28ab",
        "name": "Processing1",
        "func": "var msg1 = {};\nvar msg2 = {};\n\nmsg1.topic = \"fooTopic1\";\nmsg2.topic = \"barTopic1\";\n\nmsg1.payload = \"fooLoad1\";\nmsg2.payload = \"barLoad1\";\n\nreturn [msg1,msg2];",
        "outputs": 1,
        "noerr": 0,
        "x": 350,
        "y": 160,
        "wires": [
            [
                "a8e66620.492e48"
            ]
        ]
    },
    {
        "id": "a8e66620.492e48",
        "type": "debug",
        "z": "5824c01b.a28ab",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 530,
        "y": 160,
        "wires": []
    }
]

Node-RED v.0.20.7

这是因为功能节点可以有多个输出端口。

要从单个输出端口发送多条消息,您需要将消息用方括号括起来。

return [[msg1,msg2]]

这是因为单个数组告诉 Node-RED 从第一个端口发送 msg1,从第二个端口发送 msg2

使用二维数组表示从端口 1 发送 msg1 & msg2

这取决于您的需求。您要将多条消息发送到不同的目的地还是同一个目的地?

(A) 多个消息,不同的目的地

您需要设置多个输出(例如两个输出),并使用return [msg1,msg2];。完整代码:

var msg1 = {};
var msg2 = {};

msg1.topic = "fooTopicA";
msg2.topic = "barTopicA";

msg1.payload = "fooLoadA";
msg2.payload = "barLoadA";

return [msg1,msg2];

(B) 多条消息,单一目的地

你不需要设置多个输出,你只需要一个。在您的函数节点中使用 return [[msg1,msg2]];。完整代码:

var msg1 = {};
var msg2 = {};

msg1.topic = "fooTopicB";
msg2.topic = "barTopicB";

msg1.payload = "fooLoadB";
msg2.payload = "barLoadB";

return [[msg1,msg2]];

完整的测试流程:

[{"id":"5da1a303.2f8bbc","type":"tab","label":"test","disabled":false,"info":""},{"id":"2b48081b.61be58","type":"inject","z":"5da1a303.2f8bbc","name":"Input","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":300,"wires":[["c7169d64.37036"]]},{"id":"c7169d64.37036","type":"function","z":"5da1a303.2f8bbc","name":"Processing B","func":"var msg1 = {};\nvar msg2 = {};\n\nmsg1.topic = \"fooTopicB\";\nmsg2.topic = \"barTopicB\";\n\nmsg1.payload = \"fooLoadB\";\nmsg2.payload = \"barLoadB\";\n\nreturn [[msg1,msg2]];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":300,"wires":[["6b11270f.3477a8"]]},{"id":"6b11270f.3477a8","type":"debug","z":"5da1a303.2f8bbc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":550,"y":300,"wires":[]},{"id":"6cd03253.f1e09c","type":"inject","z":"5da1a303.2f8bbc","name":"Input","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":180,"wires":[["d9b50766.e9c9a8"]]},{"id":"d9b50766.e9c9a8","type":"function","z":"5da1a303.2f8bbc","name":"Processing A","func":"var msg1 = {};\nvar msg2 = {};\n\nmsg1.topic = \"fooTopicA\";\nmsg2.topic = \"barTopicA\";\n\nmsg1.payload = \"fooLoadA\";\nmsg2.payload = \"barLoadA\";\n\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":370,"y":180,"wires":[["abde843d.cbf888"],["e99faf37.fe8b3"]]},{"id":"abde843d.cbf888","type":"debug","z":"5da1a303.2f8bbc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":550,"y":160,"wires":[]},{"id":"e99faf37.fe8b3","type":"debug","z":"5da1a303.2f8bbc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":550,"y":220,"wires":[]}]