I'm asking here because if I don't ask, I won't get an answer.
Yes, I can test it myself but going on past history for me I still need to ask. (Otherwise I would do just that and it will fail so I have to ask all the same.)
Code:
const channels = flow.get("channels");
let x = channels[msg.payload].number;
//msg.payload = x.number;
msg.payload = x;
return msg;
This is an exert from the flow"chanels":
(ok, from the function node that sets it.)
Hi @Trying_to_learn
You can apply Optional Chaining to stop an exception.
let x = channels[msg.payload]?.number; /* x will be undefined, if the key is not found */
if(x !== undefined){
// do something with x
}
I have used !== undefined as 0 can be falsey, so if you happen to have a number of 0, it will evaluate to false, so instead ACTUALLY check if the type is undefined
Yup, what I put is basically the same thing, just slightly shorter,
let x = channels[msg.payload]?.number; /* x will be undefined, if the key is not found */
if(x !== undefined){
// do something with x
}
else{
throw("Channel not found");
}