# Can't use message element with Function node

**URL:** <https://discourse.nodered.org/t/cant-use-message-element-with-function-node/94163>\
**Category:** General\
**Tags:** function-node\
**Created:** [25 December 2024 20:04 UTC](https://discourse.nodered.org/t/cant-use-message-element-with-function-node/94163 "2024-12-25T20:04:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphjy](https://avatars.discourse-cdn.com/v4/letter/r/47e85d/32.png) [@ralphjy](https://discourse.nodered.org/u/ralphjy)\
**Post date:** [25 December 2024 20:04 UTC](https://discourse.nodered.org/t/cant-use-message-element-with-function-node/94163/1 "2024-12-25T20:04:20Z")

</div>

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.

Here is the JSON object:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/3/63d522fba2d82a5bdb5e2b299b1ae37ba68c1115.png)

As a simple test I tried to copy the value of box\_left into a new variable x.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/3/731f71de9c49e5e6411a14f47e55e57c9cec862a.png)

This resulted in an error:  
TypeError: Cannot read properties of undefined (reading 'box\_left')

I used the debug node to verify that the element that I'm trying to use had the correct path i.e. payload[0].box\_left

I then used a Change node to do the same thing and it worked.

I then tried to use Javascript to do a computation in the Change node and that also worked.

Apologies but being a new user I wasn't able to upload the screen captures of the change nodes and flow (limited to 2 media per post).

Can someone help point out what I'm doing wrong with the Function node?

I am using Node-Red v3.1.3 on a Raspberry Pi 4.

Thanks

---

<div class="post-metadata">

**Author:** ![ralphjy](https://avatars.discourse-cdn.com/v4/letter/r/47e85d/32.png) [@ralphjy](https://discourse.nodered.org/u/ralphjy)\
**Post date:** [25 December 2024 20:08 UTC](https://discourse.nodered.org/t/cant-use-message-element-with-function-node/94163/2 "2024-12-25T20:08:15Z")

</div>

Here is the Change node that worked

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/40165ac523568d6b5e6fcfbd3f6c393f84eda8ce.png)

and the one that did the computation

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/6/46bba26cd5ba63c9b58486c4d967cf7c4127818b.png)

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [25 December 2024 22:09 UTC](https://discourse.nodered.org/t/cant-use-message-element-with-function-node/94163/3 "2024-12-25T22:09:14Z")

</div>

AT a guess you are getting msg.payloads that do not have the required Object. Try

```auto
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

---

<div class="post-metadata">

**Author:** ![ralphjy](https://avatars.discourse-cdn.com/v4/letter/r/47e85d/32.png) [@ralphjy](https://discourse.nodered.org/u/ralphjy)\
**Post date:** [25 December 2024 22:43 UTC](https://discourse.nodered.org/t/cant-use-message-element-with-function-node/94163/4 "2024-12-25T22:43:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [26 December 2024 05:30 UTC](https://discourse.nodered.org/t/cant-use-message-element-with-function-node/94163/5 "2024-12-26T05:30:55Z")

</div>

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)

Craig

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 January 2025 05:31 UTC](https://discourse.nodered.org/t/cant-use-message-element-with-function-node/94163/6 "2025-01-09T05:31:06Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
