OS是Windows 10 / Node-Red版本是1.0.3 / node.js是v12.14.0
我正在尝试将多个msg.payload.subobjects处理到一个功能节点。但是这样做的时候,我总是收到错误“” TypeError:无法读取未定义的属性“ 0”“。
这些节点提供了正确的数据(在调试节点中进行了测试)。但是将它们传递给函数会返回错误。
是否可以处理一个函数的两个输入?因为它们都使用msg.payload对象?
这里是准备测试的版本:
[
{
"id":"dd09b1ba.0e51e",
"type":"tab",
"label":"Flow 3",
"disabled":false,
"info":""
},
{
"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\npredictWeatherByBees = (c0, c1, c2) => {\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 // wait 24hrs\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"
]
]
},
{
"id":"66456664.9fdde8",
"type":"function",
"z":"dd09b1ba.0e51e",
"name":"convert",
"func":"msg.payload.newWt = msg.payload[0].weather;\n\nreturn msg;",
"outputs":1,
"noerr":0,
"x":590,
"y":334,
"wires":[
[
"bb712a26.743298",
"894fec71.7d972"
]
]
},
{
"id":"63499119.238f9",
"type":"function",
"z":"dd09b1ba.0e51e",
"name":"convert",
"func":"msg.payload.data = msg.payload;\n\nreturn msg;",
"outputs":1,
"noerr":0,
"x":580,
"y":400,
"wires":[
[
"a9e4dd36.5621e",
"894fec71.7d972"
]
]
},
{
"id":"3ee84278.8a86de",
"type":"debug",
"z":"dd09b1ba.0e51e",
"name":"",
"active":true,
"tosidebar":true,
"console":false,
"tostatus":false,
"complete":"payload.weatherPredict",
"targetType":"msg",
"x":1060,
"y":360,
"wires":[
]
},
{
"id":"21f342f9.838b4e",
"type":"inject",
"z":"dd09b1ba.0e51e",
"name":"",
"topic":"",
"payload":"",
"payloadType":"date",
"repeat":"",
"crontab":"",
"once":false,
"onceDelay":0.1,
"x":180,
"y":380,
"wires":[
[
"8995ed2d.942e5",
"ad132b7f.6096e8"
]
]
},
{
"id":"8995ed2d.942e5",
"type":"function",
"z":"dd09b1ba.0e51e",
"name":"object",
"func":"msg.payload = [{\"Name\":\"C0\",\"CO2\":10}, {\"Name\":\"C1\",\"CO2\":15}, {\"Name\":\"C2\",\"CO2\":20}];\n\nreturn msg;",
"outputs":1,
"noerr":0,
"x":390,
"y":400,
"wires":[
[
"63499119.238f9"
]
]
},
{
"id":"bb712a26.743298",
"type":"debug",
"z":"dd09b1ba.0e51e",
"name":"",
"active":true,
"tosidebar":true,
"console":false,
"tostatus":false,
"complete":"payload.newWt",
"targetType":"msg",
"x":800,
"y":280,
"wires":[
]
},
{
"id":"a9e4dd36.5621e",
"type":"debug",
"z":"dd09b1ba.0e51e",
"name":"",
"active":true,
"tosidebar":true,
"console":false,
"tostatus":false,
"complete":"payload.data[0].CO2",
"targetType":"msg",
"x":810,
"y":440,
"wires":[
]
},
{
"id":"ad132b7f.6096e8",
"type":"function",
"z":"dd09b1ba.0e51e",
"name":"object",
"func":"msg.payload = [{\"weather\": \"mist\"}];\n\nreturn msg;",
"outputs":1,
"noerr":0,
"x":390,
"y":340,
"wires":[
[
"66456664.9fdde8"
]
]
}
]
问题是Weather Prediction
功能节点正在接收2条具有不同结构的独立消息。拆分流时,最终会得到传递到每个分支的消息副本。将它们都连接到功能节点时,它们不会自动合并。
您正在尝试访问功能节点中的两个新消息的一部分。如果您需要两个消息都可以完成此工作,则需要在功能节点之前插入一个join
节点,设置为等待2条消息并合并msg.payload
值