While 节点红色循环

While Loop in Node Red

我有一个连接到一些传感器的系统。主要工作是在一个或多个传感器打开时发送错误编号。

为了打印错误描述,我有一个包含两列的 csv 文件,第一列包含数字,第二列包含描述。

为了完成这项任务,我在 Node-Red 中创建了一个流程:

  1. 在一行中,我将来自系统的错误编号数组保存在一个流变量 (RandomNum) 中。
  2. 在另一行中,我读取了带有错误描述的 csv 文件,将其转换为对象数组,然后在功能节点中进行搜索以查找错误编号和描述。

搜索功能以对象数组为入口。我将流变量 (RandomNum) 保存在一个数组 (ranNum) 中,并定义了一个新数组 (newMsg),我在其中保存了错误的描述。 然后我使用 while 循环遍历对象数组,并将该数组第一列的值与 ranNum 中的数字进行比较。然后我将错误的描述保存在数组 newMsg 中,并对 ranNum 数组的每个值执行此 while 循环。

这是我的问题。当我执行流程时,它对数组 ranNum 的第一个值正常工作,但仅适用于此。我只得到 ranNum 数组中第一个数字的描述作为有效载荷,看起来 while 循环只使用了一次,然后它中断并给我数组 newMsg.

我查到Conetxt的时候,RandomNum是一个数组,

当我要求获取数组ranNum时,它也是一个数组,

我已经检查了 while 循环至少 20 次,但我没有找到它不起作用的原因。

谁能帮我解决这个问题?流变量有问题还是JavaScript和我没发现的NodeJS有很大区别?

这是我创建的流程:

[
{
    "id": "b1d8b61d0ed5a5da",
    "type": "tab",
    "label": "Flow Test",
    "disabled": false,
    "info": ""
},
{
    "id": "d677b342ccc51f1b",
    "type": "inject",
    "z": "b1d8b61d0ed5a5da",
    "name": "",
    "props": [
        {
            "p": "payload"
        },
        {
            "p": "topic",
            "vt": "str"
        }
    ],
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "topic": "",
    "payload": "",
    "payloadType": "date",
    "x": 140,
    "y": 280,
    "wires": [
        [
            "e2d5187d2e705892",
            "a167b5a81e0b36ba"
        ]
    ]
},
{
    "id": "7aa6e0bcbdf67c1c",
    "type": "file in",
    "z": "b1d8b61d0ed5a5da",
    "name": "ReadAlarms",
    "filename": "/home/DMT/Dokumente/DB_Test/alarms.csv",
    "format": "utf8",
    "chunk": false,
    "sendError": false,
    "encoding": "none",
    "allProps": false,
    "x": 530,
    "y": 280,
    "wires": [
        [
            "f2ab599d26fd7ca5",
            "94ae4b1ea29d9fe2"
        ]
    ]
},
{
    "id": "f2ab599d26fd7ca5",
    "type": "debug",
    "z": "b1d8b61d0ed5a5da",
    "name": "",
    "active": false,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "false",
    "statusVal": "",
    "statusType": "auto",
    "x": 750,
    "y": 220,
    "wires": []
},
{
    "id": "94ae4b1ea29d9fe2",
    "type": "csv",
    "z": "b1d8b61d0ed5a5da",
    "name": "ConvertAlarms",
    "sep": ",",
    "hdrin": false,
    "hdrout": "all",
    "multi": "mult",
    "ret": "\n",
    "temp": "",
    "skip": "0",
    "strings": true,
    "include_empty_strings": "",
    "include_null_values": "",
    "x": 760,
    "y": 280,
    "wires": [
        [
            "9a3547cc81c2d2bb",
            "ff9175d01efa5ce4"
        ]
    ]
},
{
    "id": "9a3547cc81c2d2bb",
    "type": "debug",
    "z": "b1d8b61d0ed5a5da",
    "name": "",
    "active": false,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "false",
    "statusVal": "",
    "statusType": "auto",
    "x": 990,
    "y": 220,
    "wires": []
},
{
    "id": "1a270df6276485c4",
    "type": "debug",
    "z": "b1d8b61d0ed5a5da",
    "name": "",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "payload",
    "targetType": "msg",
    "statusVal": "",
    "statusType": "auto",
    "x": 1230,
    "y": 220,
    "wires": []
},
{
    "id": "ff9175d01efa5ce4",
    "type": "function",
    "z": "b1d8b61d0ed5a5da",
    "name": "SearchFailureS7",
    "func": "let i=0;\nlet ranNum = [];\nranNum = flow.get(\"RandomNum\");\nlet newMsg = [];\n\nwhile (i<ranNum.length){\n\n    let n = ranNum[i];\n    newMsg[newMsg.length] = {payload: msg.payload[n].col2};\n    i = i + 1;\n    \n}\n\nreturn newMsg;\n",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 1010,
    "y": 280,
    "wires": [
        [
            "1a270df6276485c4"
        ]
    ]
},
{
    "id": "a167b5a81e0b36ba",
    "type": "function",
    "z": "b1d8b61d0ed5a5da",
    "name": "RandomArray",
    "func": "const num=[];\nvar i;\n\nfor (i=0; i<3; i++){\n    \nnum[i] = Math.floor(Math.random() * 9);\n\n}\n\nrandArray = {payload: num}\n\nreturn randArray;\n",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 340,
    "y": 400,
    "wires": [
        [
            "c011fa2387db0d18",
            "9c400785174973e9"
        ]
    ]
},
{
    "id": "c011fa2387db0d18",
    "type": "debug",
    "z": "b1d8b61d0ed5a5da",
    "name": "",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "false",
    "statusVal": "",
    "statusType": "auto",
    "x": 530,
    "y": 340,
    "wires": []
},
{
    "id": "9c400785174973e9",
    "type": "function",
    "z": "b1d8b61d0ed5a5da",
    "name": "",
    "func": "flow.set(\"RandomNum\",msg.payload);\n",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 520,
    "y": 400,
    "wires": [
        []
    ]
},
{
    "id": "e2d5187d2e705892",
    "type": "delay",
    "z": "b1d8b61d0ed5a5da",
    "name": "Delay 10ms",
    "pauseType": "delay",
    "timeout": "10",
    "timeoutUnits": "milliseconds",
    "rate": "1",
    "nbRateUnits": "1",
    "rateUnits": "second",
    "randomFirst": "1",
    "randomLast": "5",
    "randomUnits": "seconds",
    "drop": false,
    "allowrate": false,
    "outputs": 1,
    "x": 330,
    "y": 280,
    "wires": [
        [
            "7aa6e0bcbdf67c1c"
        ]
    ]
}
]

所以,我终于找到了。 while-loop 中的负载立即打破了循环。所以我使用在网上找到的类似代码更改了 while-loop 中的代码。现在可以了。

旧代码:

let i=0;
let ranNum = [];
ranNum = flow.get("RandomNum");
let newMsg = [];

while (i<ranNum.length){

    let n = ranNum[i];
    newMsg[newMsg.length] = {payload: msg.payload[n].col2};
    i = i + 1;

}

return newMsg;

新代码

let i=0;
let ranNum = [];
ranNum = flow.get("RandomNum");
let newMsg = [];

while (i<ranNum.length){

    let n = ranNum[i];
    let description = msg.payload[n].col2;
    newMsg.push(description);
    i = i + 1;

}

msg.payload = newMsg;
return msg;