# "TypeError: Cannot set properties of undefined (setting 'snr')"

**URL:** <https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575>\
**Category:** General\
**Tags:** function-node\
**Created:** [22 October 2024 09:50 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575 "2024-10-22T09:50:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roberto69](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@Roberto69](https://discourse.nodered.org/u/Roberto69)\
**Post date:** [22 October 2024 09:50 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/1 "2024-10-22T09:50:44Z")

</div>

Hello.

I have strange and interesting behaviour of function node. Script in Function node is only to form payload. In most cases it gives correct payload, but sometimes gives error "TypeError: Cannot set properties of undefined (setting 'snr')". snr parameter is always present in same format and path. Script is following:

```auto
var device_id = msg.payload.end_device_ids.device_id;
var gateway_id=msg.payload.uplink_message.rx_metadata[0].gateway_ids.gateway_id
var snr=msg.payload.uplink_message.rx_metadata[0].snr
var rssi=msg.payload.uplink_message.rx_metadata[0].rssi
var sf=msg.payload.uplink_message.settings.data_rate.lora.spreading_factor

msg.payload.uplink_message.decoded_payload["snr"] = snr
msg.payload.uplink_message.decoded_payload["rssi"] = rssi
msg.payload.uplink_message.decoded_payload["sf"] = sf
msg.payload.uplink_message.decoded_payload["gateway_id"] = gateway_id

fields=msg.payload.uplink_message.decoded_payload
tags = {
// "timestamp" : timestamp,
        "device_id" : device_id,
}
msg.payload=[fields, tags]
return msg;

```

This problem relates to snr value only.  
Is there any reasonable explanation or solution to this error?

Thank You.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 October 2024 09:59 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/2 "2024-10-22T09:59:03Z")

</div>

Hi @Roberto69

The error will be coming from the line:

```auto
msg.payload.uplink_message.decoded_payload["snr"] = snr

```

This is the first time you are accessing `msg.payload.uplink_message.decoded_payload` - so this suggests that property doesn't exist on the message the function has received.

---

<div class="post-metadata">

**Author:** ![Roberto69](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@Roberto69](https://discourse.nodered.org/u/Roberto69)\
**Post date:** [22 October 2024 10:46 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/3 "2024-10-22T10:46:13Z")

</div>

Hi @knolleary

I guess this is beacuse snr is before other parameters - if rssi would be first, than error will relate to rssi?

How can I avoid this?

---

<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:** [22 October 2024 10:53 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/4 "2024-10-22T10:53:49Z")

</div>

> [@Roberto69](#):
>
> "TypeError: Cannot set properties of undefined (setting 'snr')".

@Roberto69 that error is saying `msg.payload.uplink_message.decoded_payload` is NOT "something"

You have a few choices:

1. check it is empty & throw an error or return
2. create it if it is empty
3. skip it if empty

### 1

```js
if (!msg.payload?.uplink_message?.decoded_payload) {
    node.error("msg does not contain decoded_payload", msg)
    return
}

```

### 2

```js
if (!msg.payload?.uplink_message?.decoded_payload) {
    msg.payload.uplink_message = msg.payload.uplink_message || {}
    msg.payload.uplink_message.decoded_payload = {}
}

```

### 3

```js
if (msg.payload?.uplink_message?.decoded_payload) {
    msg.payload.uplink_message.decoded_payload.snr = snr
    msg.payload.uplink_message.decoded_payload.rssi = rssi
    msg.payload.uplink_message.decoded_payload.sf = sf
    msg.payload.uplink_message.decoded_payload.gateway_id = gateway_id
}

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 October 2024 10:54 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/5 "2024-10-22T10:54:50Z")

</div>

> [@Roberto69](#):
>
> I guess this is beacuse snr is before other parameters - if rssi would be first, than error will relate to rssi?

That is right.

Do you expect `msg.payload.uplink_message.decoded_payload` to already exist on the message or is this a new property you are wanting to create?

If its a new thing, then you can do the following to create that object with the four properties set on it:

```auto
msg.payload.uplink_message.decoded_payload = {
    snr,
    rssi,
    sf,
    gateway_id
}

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [22 October 2024 20:19 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/6 "2024-10-22T20:19:15Z")

</div>

Sorry, meant to post this sooner (before Nick posted). Contains a bit more info though so might still be useful.

> [@Roberto69](#):
>
> This problem relates to snr value only.

I don't think that is true - if you comment out the `msg.payload.uplink_message.decoded_payload["snr"] = snr` line, I think you will find the next line errors.

My suspicion is that `msg.payload.uplink_message.decoded_payload` either does not exist or is not an object.

If you add a line before that section something like:

```javascript
if (!msg?.payload?.uplink_message?.decoded_payload) {
   msg.payload.uplink_message.decoded_payload = {}
}

```

That should fix it. The `?.` are a forgiving way of checking property references. The code is a way of trying to make sure that the property exists. You might also have to add more checks if it is possible that

---

<div class="post-metadata">

**Author:** ![Roberto69](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@Roberto69](https://discourse.nodered.org/u/Roberto69)\
**Post date:** [23 October 2024 06:52 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/7 "2024-10-23T06:52:54Z")

</div>

I agree with @TotallyInformation and with @knolleary - snr is problem, but if rssi will be on the first place, problem will be with rssi.  
But just to have clear starting point - all parameters as well as payload already exists. As decoded\_payload not have all necessary parameters, metadata snr, rssi, sf and gateway\_id are added to usefull payload and converted into tabel with function node. Maybe I didn't make optimal solution to do this, but so far it works (any propose is welcome).

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

This is final payload:

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

As proposed @Steve-Mcl , choice no. 2 sounds as the most appropriate for this case.  
But it is still not clear to me why error not happening every time and I still haven't found out which condition causing error. @TotallyInformation , I'll check if msg.payload.uplink\_message.decoded\_payload exists in time of error

---

<div class="post-metadata">

**Author:** ![Roberto69](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@Roberto69](https://discourse.nodered.org/u/Roberto69)\
**Post date:** [23 October 2024 07:57 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/8 "2024-10-23T07:57:22Z")

</div>

Update ... adding only device\_id:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/a/2a750bac5d512311f0a1800403976a6f8012e4d3.png)

same error on node Add device\_id:

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

Node Add device\_id:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/2/1298e7336c96d9c9f9d00190b68160c9630bce0a.png)

Again, not always. Here is output in case there is no error:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/f/df8a7511724168ffaa50203a0c7c19205c4c0356.png)

---

<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:** [23 October 2024 08:13 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/9 "2024-10-23T08:13:08Z")

</div>

You are providing only tiny snippets of information from various things.

- There is a "debug 41" in the flow image, but the debug message you show is from "debug 76"
- The errors you show come from 2 different functions but only show the code from 1 function
- The debug output you do show is an array & will never have `.uplink_message.decoded_payload`

The cold hard immutable fact is - when you get **TypeError: Cannot set properties of undefined** it means the thing you are trying to populate is `undefined`

i.e. `msg.payload` or `msg.payload.uplink_message` or `msg.payload.uplink_messag.decode_payload` is pure and simple `undefined`.

The reason is _probably_ due to different payload shapes coming from the MQTT node, but as i stated earlier, you are providing only snippets of info. Does that MQTT node give types of payload?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 October 2024 08:13 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/10 "2024-10-23T08:13:14Z")

</div>

What is debug 41 showing when there is an error?

---

<div class="post-metadata">

**Author:** ![Roberto69](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@Roberto69](https://discourse.nodered.org/u/Roberto69)\
**Post date:** [23 October 2024 08:34 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/11 "2024-10-23T08:34:54Z")

</div>

Excuse me if I messed something.

@Colin snr parameter is present but function node shows error:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/5/054cf5766892f90a0dee66ae449a34ebdeb3eabd.png)

@Steve-Mcl  
I'm dealing with two functions - one mentioned before (Generate Payload - v4) and this one:

```auto
device_id = msg.payload.end_device_ids.device_id;
msg.payload.uplink_message.decoded_payload["device_id"] = device_id;

msg.payload = msg.payload.uplink_message.decoded_payload
return msg;

```

I can't prove yet but it seems when I have debug windows open, there is no error.

---

<div class="post-metadata">

**Author:** ![Roberto69](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@Roberto69](https://discourse.nodered.org/u/Roberto69)\
**Post date:** [23 October 2024 08:45 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/12 "2024-10-23T08:45:26Z")

</div>

Indeed, when I've opened debug window, I got an error from both function node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1e5f9f9a4635e55b4e8928fd17aa830c901ca0ae.png)

Then, no single error after 60+ messages

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [23 October 2024 08:57 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/13 "2024-10-23T08:57:46Z")

</div>

Yes, `msg.payload.uplink_message.rx_metadata[0].snr` did indeed exist and had a value of 3.75.

But did `msg.payload.uplink_message.decoded_payload` exist, when you tried to assign the value to `msg.payload.uplink_message.decoded_payload.snr` ? Was it an object?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 October 2024 16:54 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/14 "2024-10-23T16:54:00Z")

</div>

Set debug 41 to output the complete message and see what is in there when it fails.

---

<div class="post-metadata">

**Author:** ![Roberto69](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@Roberto69](https://discourse.nodered.org/u/Roberto69)\
**Post date:** [24 October 2024 06:47 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/15 "2024-10-24T06:47:32Z")

</div>

> [@jbudd](#):
>
> msg.payload.uplink\_message.decoded\_payload

As long as I see, device\_id IS defined, but function node says it is NOT.

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

@Colin did you mean this?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 October 2024 07:53 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/16 "2024-10-24T07:53:33Z")

</div>

Expand this  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/6/8613569f8e9d5862c88e3f54ea57653ca47150c1.png)

The error is not saying that `device_id` is not present, it is saying that `msg.payload.uplink_message.decoded_payload` is undefined, so it cannot set its `device_id` property.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [24 October 2024 08:00 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/17 "2024-10-24T08:00:49Z")

</div>

> [@Roberto69](#):
>
> As long as I see, device\_id IS defined, but function node says it is NOT.
> 
> "TypeError: Cannot set properties of undefined (setting 'device\_id')

Just to reinforce what everyone has already told you:  
You are misunderstanding the error message.  
It does not mean that `device_id` does not exist.  
It means that `msg.payload.uplink_message.decoded_payload` does not exist (or it exists but is not an object)

In addition I find your code rather perplexing.  
`msg.payload.uplink_message.decoded_payload["gateway_id"] = gateway_id `  
Why are you using quotes around gateway\_id inside square brackets?  
You appear to be trying to assign a value to `msg.payload.uplink_message.decoded_payload.gateway_id`  
The dot notation should work fine, provided that `msg.payload.uplink_message.decoded_payload` already exists.

---

<div class="post-metadata">

**Author:** ![Roberto69](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@Roberto69](https://discourse.nodered.org/u/Roberto69)\
**Post date:** [25 October 2024 06:37 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/18 "2024-10-25T06:37:47Z")

</div>

You're right - decoded\_payload is missing:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/0/f01c05eb2159080973f4fa8de3517dd692263d44.jpeg)

---

<div class="post-metadata">

**Author:** ![Roberto69](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@Roberto69](https://discourse.nodered.org/u/Roberto69)\
**Post date:** [25 October 2024 06:47 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/19 "2024-10-25T06:47:05Z")

</div>

@jbudd corrected, thank you a lot:

```auto
device_id = msg.payload.end_device_ids.device_id;
msg.payload.uplink_message.decoded_payload.device_id = device_id;

msg.payload = msg.payload.uplink_message.decoded_payload
return msg;

```

Thank you to all for all advices - I've found out two same devices, which beside normal uplink sends uplink without decoded\_payload. In this case, error happens.  
Is the best way to avoid such cases to use, for example, switch node - to not decode uplink message without decoded\_payload?

---

<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:** [25 October 2024 08:27 UTC](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575/20 "2024-10-25T08:27:14Z")

</div>

> [@Roberto69](#):
>
> Is the best way to avoid such cases to use, for example, switch node - to not decode uplink message without decoded\_payload?

Entirely up to you and what you need.

As for how you handle it, You have a few choices

> [@Steve-Mcl](#):
>
> You have a few choices:
> 
> 1. check it is empty & throw an error or return
> 2. create it if it is empty
> 3. skip it if empty
> 
> ### 1
> 
> ```auto
> if (!msg.payload?.uplink_message?.decoded_payload) {
> node.error("msg does not contain decoded_payload", msg)
> return
> }
> 
> ```
> 
> ### 2
> 
> ```auto
> if (!msg.payload?.uplink_message?.decoded_payload) {
> msg.payload.uplink_message = msg.payload.uplink_message || {}
> msg.payload.uplink_message.decoded_payload = {}
> }
> 
> ```
> 
> ### 3
> 
> ```auto
> if (msg.payload?.uplink_message?.decoded_payload) {
> msg.payload.uplink_message.decoded_payload.snr = snr
> msg.payload.uplink_message.decoded_payload.rssi = rssi
> msg.payload.uplink_message.decoded_payload.sf = sf
> msg.payload.uplink_message.decoded_payload.gateway_id = gateway_id
> }
> 
> ```

[Next page](https://discourse.nodered.org/t/typeerror-cannot-set-properties-of-undefined-setting-snr/92575.md?page=2)
