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": "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 ....
so I want to display all of the values from this output (104, 104, 1, 1 ....)
I am writing this function:
for(var i =0; i<Object.keys(msg.payload).length;i++)
{
msg.payload+=msg.payload[Object.keys(msg.payload)[i]].value;
}
return msg;
but I have an error:
TypeError: Object.keys called on non-object
any idea?
The problem is that your
for
loop is modifyingmsg.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 theObject.keys
call fails.You should build up your result in a new variable and set
msg.payload
at the end: