2016-06-22 1 views
0

Je travaille sur le nœud rouge (SNMP).Afficher uniquement les champs de valeur provenant du message msg.payload dans le nœud rouge

Quand je Deploy, j'ai la sortie ci-dessous:

[ { "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 ....

donc je veux afficher toutes les valeurs de cette sortie (104, 104, 1, 1 ....)

Je vous écris cette fonction:

for(var i =0; i<Object.keys(msg.payload).length;i++) 
{ 
    msg.payload+=msg.payload[Object.keys(msg.payload)[i]].value; 
} 
return msg; 

mais j'ai une erreur:

TypeError: Object.keys called on non-object

une idée?

Répondre

1

Le problème est que votre for boucle modifie msg.payload à chaque itération - et parce qu'il fait un += il le transforme en chaîne. Cela signifie que la deuxième fois à travers la boucle, msg.payload n'est plus l'objet qu'il était au début, donc l'appel Object.keys échoue.

vous devez construire votre résultat dans une nouvelle variable et mettre msg.payload à la fin:

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; 
+0

merci beaucoup. Ça marche – aline