# How to store a msg.payload in a script variable inside a red ui\_template node?

**URL:** <https://discourse.nodered.org/t/how-to-store-a-msg-payload-in-a-script-variable-inside-a-red-ui-template-node/66429>\
**Category:** General\
**Created:** [14 August 2022 23:40 UTC](https://discourse.nodered.org/t/how-to-store-a-msg-payload-in-a-script-variable-inside-a-red-ui-template-node/66429 "2022-08-14T23:40:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmino0312](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmino0312/32/66048_2.png) [@fmino0312](https://discourse.nodered.org/u/fmino0312)\
**Post date:** [14 August 2022 23:40 UTC](https://discourse.nodered.org/t/how-to-store-a-msg-payload-in-a-script-variable-inside-a-red-ui-template-node/66429/1 "2022-08-14T23:40:01Z")

</div>

I wrote this code but it doesn't work

```auto
   <script type = "text/javascript">

        var msg = {{msg.payload}}

   </script>

```

this method considers the value {{msg.payload}} as an error due to the '{' in the syntax, I tried different ways, but nothing works, I guess I'm missing something I don't understand, please I'm open to any suggestions.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [15 August 2022 01:20 UTC](https://discourse.nodered.org/t/how-to-store-a-msg-payload-in-a-script-variable-inside-a-red-ui-template-node/66429/2 "2022-08-15T01:20:33Z")

</div>

There is a code example on the **ui\_template**'s help tab of how to properly watch and  
receive a msg in javascript.

```auto
<script>
(function(scope) {
  scope.$watch('msg', function(msg) {
    if (msg) {
      // Do something when msg arrives
     // ...
    }
  });
})(scope);
</script>

```

---

<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:** [15 August 2022 08:26 UTC](https://discourse.nodered.org/t/how-to-store-a-msg-payload-in-a-script-variable-inside-a-red-ui-template-node/66429/3 "2022-08-15T08:26:28Z")

</div>

And more extensive examples elsewhere.

---

<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 October 2022 08:26 UTC](https://discourse.nodered.org/t/how-to-store-a-msg-payload-in-a-script-variable-inside-a-red-ui-template-node/66429/4 "2022-10-14T08:26:56Z")

</div>

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