Node Red help please

When using a Function Node, how do I test for the following to be null. It’s the else if line. With OwnTracks, it sends home via msg.payload.inregions[0] but what if that msg type doesnt exist.

if (msg.payload._type == "location"){
    if (msg.payload.inregions[0] == "home"){
        return {"topic": "OwnTracks", "payload": "home"}
        }
    else if (msg.payload.inregions[0] == null){
        return {"topic": "OwnTracks", "payload": "away"}
    }
    else {
        return {"topic": "OwnTracks", "payload": "away"}
        }
}

Without having sample data I can’t be certain, but it’ll likely be undefined and not null, for that “=== undefined” is used.

if (msg.payload._type == "location"){
    if (msg.payload.inregions[0] == "home"){
        return {"topic": "OwnTracks", "payload": "home"}
        }
    else if (msg.payload.inregions[0] === undefined){
        return {"topic": "OwnTracks", "payload": "away"}
    }
    else {
        return {"topic": "OwnTracks", "payload": "away"}
        }
}
1 Like