在节点红色中使用if语句后没有显示任何内容?

时间:2016-06-28 13:58:07

标签: json if-statement node-red

我有以下示例Node-RED节点代码,我试图获得“hi”。

我已经设法获得了年龄的价值,但是当我部署应用程序时,没有任何关于if语句的信息。

var data= {
     "age" : "24",      

     "hometown" : {"country": "USA",
                   "City": "Missoula, MT"},                                                         
     "gender" : "male"
 };
node.send( {payload:data.hometown.country});
node.send( {payload:data.hometown.City});
node.send({payload:data});
var s =({payload:data.age});
node.send(s);
if (s == '24'){
    node.send("hi");
}

结果如下

6/28/2016, 9:55:17 AM8ae7211a.e3f0b msg.payload : string [3] USA
6/28/2016, 9:55:17 AM8ae7211a.e3f0b msg.payload : string [12] Missoula, MT 
6/28/2016, 9:55:17 AM8ae7211a.e3f0b msg.payload : Object { "age": "24", "hometown": { "country": "USA", "City": "Missoula, MT" }, "gender": "male" } 
6/28/2016, 9:55:17 AM8ae7211a.e3f0b msg.payload : string [2] 24

我可以看到data.hometown.country,city和age的值,但我的if语句不起作用。

2 个答案:

答案 0 :(得分:0)

在Node-RED节点中,您不能只发送原始字符串"hi",您需要将其包装在带有有效负载字段的消息对象中。

...
if (s == '24'){
    node.send({payload:"hi"});
}
...

答案 1 :(得分:0)

此代码有效:

   var data= {
     "age" : 24,      

     "hometown" : {"country": "USA",
                   "City": "Missoula, MT"},                                                         
     "gender" : "male"
 };

    node.send( {payload:data.hometown.country});
    node.send( {payload:data.hometown.City});
    node.send({payload:data});
    var s =({payload:data.age});
    node.send(s);
    if (s.payload === 24){
        node.send({payload:"hi"});
    }