最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

If statement in Node red (javascript) - Stack Overflow

programmeradmin6浏览0评论

I'm trying to evaluate a value in node red :

from the join node I'm getting the following:

so what I'm trying to do in the function is to check the 1. sent value:

var payload  = msg.payload; 
if (msg.payload[0] === "0" ){
    msg.payload  =0;
} else {
    msg.payload = 1;
}

//msg.payload = payload[0];
return msg;

So my question why am I getting the if statement is false ?

thanks for any hint

I'm trying to evaluate a value in node red :

from the join node I'm getting the following:

so what I'm trying to do in the function is to check the 1. sent value:

var payload  = msg.payload; 
if (msg.payload[0] === "0" ){
    msg.payload  =0;
} else {
    msg.payload = 1;
}

//msg.payload = payload[0];
return msg;

So my question why am I getting the if statement is false ?

thanks for any hint

Share Improve this question asked Oct 8, 2018 at 8:53 EngineEngine 5,43222 gold badges93 silver badges169 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

Not sure what you are trying to check but checking the value of the payload property can be done as follows:

if(typeof msg.payload === "object"){
    if(msg.payload[0].value === 0) { // Use "0" if this value is a string, but I guess not by inspecting your data.
        // Your code
    } else {
        // Other code
    }
}
else {
    // msg.payload is not an array
}

According to your data, you have two situations, either msg.payload is an array or either it is a value.

发布评论

评论列表(0)

  1. 暂无评论