I'm having what I expect is a simple problem caused by my lack of understanding Node-Red.
I have a JSON string received via MQTT. I set the MQTT input node to output a JSON object. This is data from object detection that provides detection and bounding box info. I need to convert (do calculations) on some of the elements of the bounding box. I thought this would be simple using a Function node but I can't get it to work.
AT a guess you are getting msg.payloads that do not have the required Object. Try
let x = msg.payload[0]?.box_left ?? 'No box_left value'
msg.x = x
return msg
And stick a debug.node on the output
Edit: The ?. stops the assignment creating an error and ?? returns whatever is after it in the event that there is an 'undefined' result in the previous part of the statement
Thanks that works to suppress the TypeError. You're correct in that there is no bounding box info when there is no face detection. I guess I should logically suppress the message at the source in that case. I was confused because the Change node didn't register the same error.
What you should probably do is put a switch node in before your function code - that way you can direct the flow away from your function (possibly to another function) if it for instance does not detect a face but does detect a number plate
So you can test for the value to be there in the switch node - if it is not then send it to another branch (using either Otherwise if you do nto wish to do any secondary tests - or the conditions for your other tests on the related outputs)