Node-RED发送多个味精仅返回第一个味精

时间:2019-07-18 09:42:41

标签: javascript node.js json node-red

我正在尝试使用NodeRed发送多封邮件。

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

Flow

正在处理1个代码:

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

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

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

return [msg1,msg2];

结果:

Result Processing1

Processing2代码:

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

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

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

return msg1,msg2;

Result Processing2

我原本期望2条单独的消息,但是debug只输出一条。 我在做什么错了?

为想要自我测试的人提供帮助:

[
    {
        "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

2 个答案:

答案 0 :(得分:1)

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

要从单个输出端口发送多条消息,您需要将消息双重包装在方括号中。

return [[msg1,msg2]]

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

使用2D数组表示从端口1发送msg1msg2

答案 1 :(得分:0)

这取决于您的需求。您要将多封邮件发送到不同的目的地还是同一目的地?

(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]];

Screenshot node-red

全流程测试:

[{"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":[]}]