# "TypeError: Cannot read property 'decoded\_payload' of undefined"

**URL:** https://discourse.nodered.org/t/typeerror-cannot-read-property-decoded-payload-of-undefined/59087
**Category:** General
**Tags:** function-node
**Created:** [28 February 2022 03:21 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-decoded-payload-of-undefined/59087 "2022-02-28T03:21:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![rfrancis97](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@rfrancis97](https://discourse.nodered.org/u/rfrancis97)
#### Post date: [28 February 2022 03:21 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-decoded-payload-of-undefined/59087/1 "2022-02-28T03:21:10Z")

</div>

When I inject code:

```auto
{
    "received_at": "2022-02-27T23:45:18.013631675Z",
    "uplink_message": {
        "session_key_id": "AX88sOZsNTkoN5rZ+Rzx/Q==",
        "f_port": 1,
        "f_cnt": 80,
        "frm_payload": "SGVsbG8sIHdvcmxkIQ==",
        "decoded_payload": {
            "degreesC": 20.20263671875,
            "humidity": 38.1103515625
        }
    }
}

```

into function node:

```auto
var thing = [{
    degreesC: msg.payload.uplink_message.decoded_payload.degreesC,
    humidity: msg.payload.uplink_message.decoded_payload.humidity,
}]
msg.payload = thing;
return msg;

```

I get

```auto
2/27/2022, 7:06:04 PMnode: a99be1c0d3d3695d
msg.payload : array[1]
array[1]
0: object
degreesC: 20.20263671875
humidity: 38.1103515625

```

So everything works fine. But when I feed the following (basically the same) MQTT payload into the same function:

```auto
{
    "end_device_ids": {
        "device_id": "eui-70b3d57ed004d388",
        "application_ids": {
            "application_id": "mqtt-rpi3b"
        },
        "dev_eui": "70B3D57ED004D388",
        "join_eui": "0000000000000000",
        "dev_addr": "260C0875"
    },
    "correlation_ids": [
        "as:up:01FWYS5ZNTV0C25QZG3CZYW2WY",
        "gs:conn:01FWY7ADE93XZZ8A72N1EHDH2S",
        "gs:up:host:01FWY7ADJXWZWYHXHR5852CGS8",
        "gs:uplink:01FWYS5ZF1JRJDHCP9SDDMN718",
        "ns:uplink:01FWYS5ZF4TDHF5473B8CBK29V",
        "rpc:/ttn.lorawan.v3.GsNs/HandleUplink:01FWYS5ZF4744HNW8ZJ25RDB26",
        "rpc:/ttn.lorawan.v3.NsAs/HandleUplink:01FWYS5ZNS2ZXRQ5WT1B7GV9VX"
    ],
    "received_at": "2022-02-27T23:45:18.013631675Z",
    "uplink_message": {
        "session_key_id": "AX88sOZsNTkoN5rZ+Rzx/Q==",
        "f_port": 1,
        "f_cnt": 80,
        "frm_payload": "SGVsbG8sIHdvcmxkIQ==",
        "decoded_payload": {
            "degreesC": 20.20263671875,
            "humidity": 38.1103515625
        }
    }
}

```

I get:

```auto
2/27/2022, 7:05:34 PMnode: Test
function : (error)
"TypeError: Cannot read property 'decoded_payload' of undefined"

```

I'm just learning this stuff, so be kind.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [28 February 2022 07:29 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-decoded-payload-of-undefined/59087/2 "2022-02-28T07:29:26Z")

</div>

From what you have shown us, it seems your code and data are fine but obviously something else is going on. Lets add some debug messages to your function...

```auto

if(!msg.payload.uplink_message) {
  node.warn(["this msg does not have '.payload.uplink_message' - look closely at its content", msg]);
}

var thing = [{
    degreesC: msg.payload.uplink_message.decoded_payload.degreesC,
    humidity: msg.payload.uplink_message.decoded_payload.humidity,
}]
msg.payload = thing;
return msg;

```

---

<div class="post-metadata">

### Author: ![rfrancis97](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@rfrancis97](https://discourse.nodered.org/u/rfrancis97)
#### Post date: [28 February 2022 17:13 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-decoded-payload-of-undefined/59087/3 "2022-02-28T17:13:32Z")

</div>

I needed to "Always convert to JavaScript Object' by adding a JSON node before the function.  
Thanks!

---

<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: [14 March 2022 17:13 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-property-decoded-payload-of-undefined/59087/4 "2022-03-14T17:13:39Z")

</div>

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