【发布时间】:2020-10-01 07:57:23
【问题描述】:
而响应,假设function 只包含return msg;,是一个字典,例如:
{
"data": [{
"time": "2020-10-01",
"value": 998.37
}],
"id": "27",
"tags": []
}
现在我只想提取值本身。我怎样才能做到这一点?
如果我尝试return msg['data']['value'];,我会收到 TypeError TypeError: Cannot read property 'value' of undefined。
编辑:我尝试了 Pogrindis 方法,但在处理 Node-RED 流文件时遇到了困难。
我的 Node-RED 接口的 JSON 非常简单,函数是单行的
return (msg.payload)['data'][0]['value'];:
[{
"id": "cda42b56.ad2258",
"type": "tab",
"label": "Test",
"disabled": false,
"info": ""
}, {
"id": "95fa7937.aa8e48",
"type": "debug",
"z": "cda42b56.ad2258",
"name": "Logger",
"active": true,
"tosidebar": true,
"console": true,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 480,
"y": 80,
"wires": []
}, {
"id": "200acc6f.e2ff54",
"type": "function",
"z": "cda42b56.ad2258",
"name": "",
"func": "return (msg.payload)['data'][0]['value'];\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"x": 300,
"y": 80,
"wires": [
["95fa7937.aa8e48"]
]
}, {
"id": "831bfe43.3d956",
"type": "inject",
"z": "cda42b56.ad2258",
"name": "",
"props": [{
"p": "payload"
}],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "{\"data\":[{\"time\":\"2020\",\"value\":997.62}],\"id\":\"xy123\"}",
"payloadType": "str",
"x": 120,
"y": 80,
"wires": [
["200acc6f.e2ff54"]
]
}]
【问题讨论】:
标签: javascript node-red