仅在节点红色中显示来自 msg.payload 的值字段 [英] display only value fields from the msg.payload in node red
问题描述
我正在研究节点 red (SNMP).
I am working on node red (SNMP).
当我部署时,我有以下输出:
When I deploy, I have the output below:
[ { "oid": "1.3.6.1.2.1.10.21.1.2.1.1.2.1.26", "type": 2, "value":104, "tstr": "Integer" }, { "oid": "1.3.6.1.2.1.10.21.1.2.1.1.2.2.27", "type": 2, "value":104,"tstr": "Integer" }, { "oid": "1.3.6.1.2.1.10.21.1.2.1.1.2.10.28","type": 2, "value": 1, "tstr": "Integer" }, { "oid":"1.3.6.1.2.1.10.21.1.2.1.1.2.11.29", "type": 2, "value": 1, "tstr":"整数" }, { "oid": "1.3.6.1.2.1.10.21.1.2.1.1.2.12.30", "type": 2,"value": 1, "tstr": "Integer" }, { "oid":"1.3.6.1.2.1.10.21.1.2.1.1.2.13.31", "type": 2, "value": 1,"tstr": "Integer" }, { "oid": "1.3.6.1.2.1.10.21.1.2.1.1.2.14.32","type": 2, "value": 101, "tstr": "Integer" }, { "oid":"1.3.6.1.2.1.10.21.1.2.1.1.2.15.38", "type": 2, "value": 1,"tstr": "Integer" }, { "oid": "1.3.6.1.2.1.10.21.1.2.1.1.2.100.39","type": 2, "value": 101, "tstr": "Integer" }, { "oid":"1.3.6.1.2.1.10.21.1.2.1.1.2.101.40", "type": 2, "value": 101,"tstr": "我......
[ { "oid": "1.3.6.1.2.1.10.21.1.2.1.1.2.1.26", "type": 2, "value": 104, "tstr": "Integer" }, { "oid": "1.3.6.1.2.1.10.21.1.2.1.1.2.2.27", "type": 2, "value": 104, "tstr": "Integer" }, { "oid": "1.3.6.1.2.1.10.21.1.2.1.1.2.10.28", "type": 2, "value": 1, "tstr": "Integer" }, { "oid": "1.3.6.1.2.1.10.21.1.2.1.1.2.11.29", "type": 2, "value": 1, "tstr": "Integer" }, { "oid": "1.3.6.1.2.1.10.21.1.2.1.1.2.12.30", "type": 2, "value": 1, "tstr": "Integer" }, { "oid": "1.3.6.1.2.1.10.21.1.2.1.1.2.13.31", "type": 2, "value": 1, "tstr": "Integer" }, { "oid": "1.3.6.1.2.1.10.21.1.2.1.1.2.14.32", "type": 2, "value": 101, "tstr": "Integer" }, { "oid": "1.3.6.1.2.1.10.21.1.2.1.1.2.15.38", "type": 2, "value": 1, "tstr": "Integer" }, { "oid": "1.3.6.1.2.1.10.21.1.2.1.1.2.100.39", "type": 2, "value": 101, "tstr": "Integer" }, { "oid": "1.3.6.1.2.1.10.21.1.2.1.1.2.101.40", "type": 2, "value": 101, "tstr": "I ....
所以我想显示这个输出中的所有值 (104, 104, 1, 1 ....)
so I want to display all of the values from this output (104, 104, 1, 1 ....)
我正在编写这个函数:
for(var i =0; i<Object.keys(msg.payload).length;i++)
{
msg.payload+=msg.payload[Object.keys(msg.payload)[i]].value;
}
return msg;
但我有一个错误:
TypeError: Object.keys 在非对象上调用
TypeError: Object.keys called on non-object
有什么想法吗?
推荐答案
问题是您的 for
循环在每次迭代时都在修改 msg.payload
- 因为它正在做一个 +=
它正在把它变成一个字符串.这意味着第二次循环时,msg.payload
不再是它在开始时的对象,因此 Object.keys
调用失败.
The problem is that your for
loop is modifying msg.payload
on each iteration - and because it is doing a +=
it is turning it into a String. That means the second time through the loop, msg.payload
is no longer the object it was at the start so the Object.keys
call fails.
你应该在一个新变量中建立你的结果并在最后设置msg.payload
:
You should build up your result in a new variable and set msg.payload
at the end:
var result = [];
var keys = Object.keys(msg.payload);
for(var i =0; i<keys.length;i++)
{
result.push(msg.payload[keys[i]].value);
}
// At this point, result is an array of the values you want
// You can either return it directly with:
// msg.payload = result;
// or, if you want a string representation of the values as a
// comma-separated list:
// msg.payload = result.join(", ");
return msg;
这篇关于仅在节点红色中显示来自 msg.payload 的值字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!