# TypeError: Cannot read properties of undefined (reading 'notification')

**URL:** <https://discourse.nodered.org/t/typeerror-cannot-read-properties-of-undefined-reading-notification/60995>\
**Category:** General\
**Tags:** function-node\
**Created:** [8 April 2022 17:47 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-properties-of-undefined-reading-notification/60995 "2022-04-08T17:47:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mannshoch](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mannshoch/32/19477_2.png) [@Mannshoch](https://discourse.nodered.org/u/Mannshoch)\
**Post date:** [8 April 2022 17:47 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-properties-of-undefined-reading-notification/60995/1 "2022-04-08T17:47:41Z")

</div>

On a function I have this two Ifs:

```auto
if (typeof msg.payload.notification !== 'undefined') {...}
if (typeof msg.payload.data.data.item.title !== 'undefined') {...}

```

But I get the Error `TypeError: Cannot read properties of undefined (reading 'notification')`

What did I wrong? How should I do it better?

---

<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:** [8 April 2022 18:39 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-properties-of-undefined-reading-notification/60995/2 "2022-04-08T18:39:49Z")

</div>

Either your msg doesn't have a payload or you have deleted it.

What is in the rest of the function? Specifically above that code?

Try adding `node.warn(["msg",msg])` before that code to see what is actually inside `msg`

---

<div class="post-metadata">

**Author:** ![Mannshoch](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mannshoch/32/19477_2.png) [@Mannshoch](https://discourse.nodered.org/u/Mannshoch)\
**Post date:** [9 April 2022 22:10 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-properties-of-undefined-reading-notification/60995/3 "2022-04-09T22:10:57Z")

</div>

I resceive diferent message from an external source. So in this function I separate the kind of messages and react on it. I know that there are messages without a 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:** [9 April 2022 22:19 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-properties-of-undefined-reading-notification/60995/4 "2022-04-09T22:19:04Z")

</div>

> [@Mannshoch](#):
>
> I know that there are messages without a payload

If you are know you can get messages without a payload, then that is why you get the error `TypeError: Cannot read properties of undefined (reading 'notification')`

It is literally saying "Cannot read `notification` from `nothing`" - so you must guard against trying to access `notification` if its source is empty

e.g...

```auto
//GUARD: check that msg.payload is SOMETHING before trying to read its properties...
if(typeof msg.payload == "object") {
    if (typeof msg.payload.notification !== 'undefined') {...}
     //GUARD: check that msg.payload.data.data.item is SOMETHING before trying to read its properties...
    if (msg.payload.data && msg.payload.data.data && msg.payload.data.data.item) {
        if (typeof msg.payload.data.data.item.title !== 'undefined') {...}
        //...
        //etc
    }
    //...
    //etc
}

```

---

<div class="post-metadata">

**Author:** ![Mannshoch](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mannshoch/32/19477_2.png) [@Mannshoch](https://discourse.nodered.org/u/Mannshoch)\
**Post date:** [11 April 2022 21:26 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-properties-of-undefined-reading-notification/60995/5 "2022-04-11T21:26:07Z")

</div>

Thanks a lot @Steve-Mcl I was not aware that Nodered is that picky about missing content. Your answer was Helpfull !

---

<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:** [10 June 2022 21:26 UTC](https://discourse.nodered.org/t/typeerror-cannot-read-properties-of-undefined-reading-notification/60995/6 "2022-06-10T21:26:40Z")

</div>

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