2

I'm trying to evaluate a value in node red : enter image description here

from the join node I'm getting the following:

enter image description here

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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Allow me to add one explanation here for some who don't know. msg.payload is actually an array but with "typeof" it will return "object". Reference: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.