功能/节点红色的多个输入

Multiple inputs in function / node-red

OS 是 Windows 10 / Node-Red 版本是 1.0.3 / node.js 是 v12.14.0

我正在尝试将多个 msg.payload.subobjects 处理到一个函数节点。但是这样做时,我总是会收到错误消息“"TypeError: Cannot read property '0' of undefined"”。

节点正在提供正确的数据(在调试节点中测试)。但是将它们传递给函数 returns 错误。

是否可以像这样处理一个函数的两个输入?因为他们都使用 msg.payload 对象?

这是准备好的测试版本:

[ 
   { 
      "id":"894fec71.7d972",
      "type":"function",
      "z":"dd09b1ba.0e51e",
      "name":"Weather Prediction",
      "func":"var wBees = 0;\nvar wActually = msg.payload.newWt;\n\nvar c0 = msg.payload.data[0].CO2;\nvar c1 = msg.payload.data[1].CO2;\nvar c2 = msg.payload.data[2].CO2;\n\n//wActually.localeCompare(\"rain\") === false\nif(wActually === \"rain\"){\n    wActually = \"good\";\n}else{\n    wActually = \"other\";\n}\n\n//c0, c1, c2 --- 2pm, 10pm, 6am\n  var day = c1-c0;\n  var night = c1-c2;\n\n// Checks proportion\n  if(night > day){\n    // bad weather\n\twBees = \"other\";\n  }else{\n    // good weather\n\twBees = \"good\";\n  }\n\n// Evaluation\nif(wBees == wActually){\n    msg.payload.weatherPredict = \"Correct\" + \" || Current: \" + msg.payload.weather;\n\treturn msg;\n}else{\n\tmsg.payload.weatherPredict = \"Incorrect\" + \" || Current: \" + msg.payload.weather;\n\treturn msg;\n}",
      "outputs":1,
      "noerr":0,
      "x":830,
      "y":360,
      "wires":[ 
         [ 
            "3ee84278.8a86de"
         ]
      ]
   }
]

问题是您的 Weather Prediction 功能节点正在接收 2 条不同结构的单独消息。当您拆分流时,您最终会得到一份传递到每个分支的消息副本。当您将它们连接到函数节点时,它们不会自动合并。

您正在尝试访问功能节点中两条新消息的部分内容。如果您需要两条消息来完成这项工作,您需要在函数节点之前插入一个 join 节点,设置为等待 2 条消息并合并 msg.payload