I'm quite new to node-red and also JS, but for the moment I'm quite happy with what I could achieve ( I focus on making workflows and custom panels for video broadcast devices)
I try to make use as much as possible of function node, as it gives me most control on the code , just using change nodes when I (believe I) don't have an easy alternative.
Problem I have in fucntion node quite often, is dealing with arguments coming into the node . Example :
if I use context.set("test", msg.payload.XXXX); I get a error telling XXX is undefined.
Now, if I use :
payload = msg.payload;
context.set("test", payload.XXXX) : I'm fine .
Is that the expected behaviour ? I guess so, I miss something there for a full understanding....
In a function node, msg is the incoming object. payload is a sub property of msg If msg.payload.XXXX throws that error then it simply doesn't exist in the msg.payload. something else must be going on. E.g. you are sending a different msg or you overwrite something before that line.
Posting your full function and details of what goes into it would help us help you.
Ps, both versions of your code should work (however to minimise bugs, you should define the payload variable using let or const)
thanks for your answer. I still don't really understand, as I explained in first post the msg.payload being assigned to a "payload" variable ( could be anything else there) is the only difference between working and not-working code....