# Pass msg.payload.property to template in mustaches - Bug?

**URL:** https://discourse.nodered.org/t/pass-msg-payload-property-to-template-in-mustaches-bug/99846
**Category:** Dashboard
**Tags:** dashboard-2
**Created:** [2 December 2025 17:43 UTC](https://discourse.nodered.org/t/pass-msg-payload-property-to-template-in-mustaches-bug/99846 "2025-12-02T17:43:02Z")
**Posts on this page:** 3
**Page:** 1

<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: [2 December 2025 17:43 UTC](https://discourse.nodered.org/t/pass-msg-payload-property-to-template-in-mustaches-bug/99846/1 "2025-12-02T17:43:02Z")

</div>

This template works without error when passed payload `"Tuesday"` :

```auto
<template>
    <div>
        <h2>No errors here</h2>
       <p>Today is {{msg.payload}}</p>
    </div>
</template>

```

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

But this template gives a console error `errorCaptured TypeError: can't access property "dinner", msg.payload is undefined` when passed payload

```auto
{
    "breakfast": "Croissant",
    "lunch": "Sandwiches",
    "dinner": "Steak"
}

```

```auto
<template>
    <div>
        <h2>Check the console!</h2>
       <p>Dinner will be {{msg.payload.dinner}}</p>
    </div>
</template>

```

NB the dashboard still shows the desired output  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/488870700fcef8efc901196bb14fd20c893c3fef.png)

It seems like the code is initially parsed before msg.payload has a value, then again when the message arrives. But that doesn't explain why there is no error in the first example.

**Is this a bug or deliberate?**

There are a bunch of work-arounds. Probably the simplest is to use "Optional chaining" to pass message properties. NB the fallback value (here an empty string) is essential. :

```auto
<p>Dinner will be {{msg?.payload?.dinner ?? '' }}</p>

```

**Is there any reason why the ui-template code can't do this optional chaining, or protect the object with v-if, or otherwise, automatically in the background?**

---

<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: [2 December 2025 21:09 UTC](https://discourse.nodered.org/t/pass-msg-payload-property-to-template-in-mustaches-bug/99846/2 "2025-12-02T21:09:10Z")

</div>

> [@jbudd](#):
>
> Is this a bug or deliberate?

It is inevitable. The code has to be interpreted immediately it appears on screen, the template might not have any dynamic content of that sort so might never be sent a message. The reason there is not a problem with `msg.something` is, I guess, that, due to the way the template widget is coded, an empty `msg` object does already exist in the browser, ready for a real message to be sent across from the server. Therefore `msg.payload` is not an error, just undefined. However if you add another level on then the js interpreter throws an error as you are trying to evaluate `undefined.dinner`. The solution (which is not a workaround, it is just good practice) is use the optional chaining as you suggest. You don't need it on the `msg` part though, as that does exist.

> [@jbudd](#):
>
> NB the fallback value (here an empty string) is essential

I am not seeing that. For me it works ok without it.  
`{{msg.payload?.dinner'}}`

---

<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: [1 January 2026 21:09 UTC](https://discourse.nodered.org/t/pass-msg-payload-property-to-template-in-mustaches-bug/99846/3 "2026-01-01T21:09:36Z")

</div>

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