# Msg.payload to template node

**URL:** https://discourse.nodered.org/t/msg-payload-to-template-node/92110
**Category:** Dashboard
**Created:** [1 October 2024 15:44 UTC](https://discourse.nodered.org/t/msg-payload-to-template-node/92110 "2024-10-01T15:44:31Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![LucasPelu](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@LucasPelu](https://discourse.nodered.org/u/LucasPelu)
#### Post date: [1 October 2024 15:44 UTC](https://discourse.nodered.org/t/msg-payload-to-template-node/92110/1 "2024-10-01T15:44:31Z")

</div>

I am working on loading data to mysql using a dashboard. I want that as soon as the data is loaded correctly, a floating message appears saying that the data has been loaded correctly.  
To do this, I used the affectedRow variable returned by the mysql node. I created the floating message, but the template node does not detect the msg.payload.affectedrow. Here I leave a simplification of the flow so that you can see where my error is.  
greetings and thanks  
`[{ "id": "88bc9b7989643e09", "type": "inject", "z": "cb00c3a841285e40", "name": "", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" }], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "{ \"affectedRows\": 1 } ", "payloadType": "json", "x": 650, "y": 300, "wires": [[ "781b3fffc708a025"] ] } ]`

---

<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: [1 October 2024 16:05 UTC](https://discourse.nodered.org/t/msg-payload-to-template-node/92110/2 "2024-10-01T16:05:51Z")

</div>

Your flow contains only the Inject node. We need to see the template node too.

---

<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: [1 October 2024 16:06 UTC](https://discourse.nodered.org/t/msg-payload-to-template-node/92110/3 "2024-10-01T16:06:58Z")

</div>

> [@LucasPelu](#):
>
> the template node does not detect the msg.payload.affectedrow

It is `msg.payload.affectedRows`, not `msg.payload.affectedrow`

---

<div class="post-metadata">

### Author: ![LucasPelu](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@LucasPelu](https://discourse.nodered.org/u/LucasPelu)
#### Post date: [1 October 2024 16:10 UTC](https://discourse.nodered.org/t/msg-payload-to-template-node/92110/4 "2024-10-01T16:10:08Z")

</div>

sorry check it now

```auto
[
    {
        "id": "6b94d4a4c0e3829a",
        "type": "debug",
        "z": "cb00c3a841285e40",
        "name": "debug 14",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1040,
        "y": 300,
        "wires": []
    },
    {
        "id": "781b3fffc708a025",
        "type": "ui_template",
        "z": "cb00c3a841285e40",
        "group": "fa937a70f3302944",
        "name": "",
        "order": 2,
        "width": 0,
        "height": 0,
        "format": "<!-- Contenedor del mensaje flotante -->\n<div id=\"floatingMessage\" class=\"floating-message\" style=\"display: none;\"></div>\n\n<script>\n (function(scope) {\n // Función para mostrar el mensaje flotante\n function showFloatingMessage(text, color) {\n const floatingMessage = document.getElementById('floatingMessage');\n floatingMessage.style.display = 'block';\n floatingMessage.textContent = text;\n floatingMessage.style.color = color;\n\n // Ocultar el mensaje después de 3 segundos\n setTimeout(function() {\n floatingMessage.style.display = 'none';\n }, 3000);\n }\n\n // Escuchar el mensaje que proviene del nodo inject\n scope.on('msg', function(msg) {\n console.log('Mensaje recibido en el template:', msg); // Verificar que se recibe el mensaje\n\n // Asegurarse de que msg.payload y msg.payload.affectedRows existan\n if ({{payload}} && {{payload.affectedRows}} !== undefined) {\n // Imprimir el número de affectedRows en la consola\n console.log('Affected Rows:', {{msg.payload.affectedRows}}); \n\n if ({{payload.affectedRows}} !== 0) {\n showFloatingMessage('Los datos fueron cargados exitosamente.', 'green');\n } else {\n showFloatingMessage('No se pudieron cargar los datos.', 'red');\n }\n } else {\n // Mensaje de advertencia si no se recibe affectedRows\n showFloatingMessage('Error: affectedRows no definido.', 'red');\n }\n });\n })(scope);\n</script>\n\n<style>\n .floating-message {\n position: fixed;\n top: 0;\n left: 50%;\n transform: translateX(-50%);\n padding: 15px;\n background-color: rgba(255, 255, 255, 0.9);\n border: 2px solid #ccc;\n border-radius: 5px;\n z-index: 1000;\n text-align: center;\n font-weight: bold;\n box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);\n }\n</style>",
        "storeOutMessages": true,
        "fwdInMessages": true,
        "resendOnRefresh": true,
        "templateScope": "local",
        "className": "",
        "x": 860,
        "y": 300,
        "wires": [
            [
                "6b94d4a4c0e3829a"
            ]
        ]
    },
    {
        "id": "88bc9b7989643e09",
        "type": "inject",
        "z": "cb00c3a841285e40",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{ \"affectedRows\": 1 } ",
        "payloadType": "json",
        "x": 650,
        "y": 300,
        "wires": [
            [
                "781b3fffc708a025"
            ]
        ]
    },
    {
        "id": "fa937a70f3302944",
        "type": "ui_group",
        "name": "Prueba Carga",
        "tab": "8c22a23a1f8fc122",
        "order": 1,
        "disp": true,
        "width": 30,
        "collapse": false,
        "className": ""
    },
    {
        "id": "8c22a23a1f8fc122",
        "type": "ui_tab",
        "name": "prueba",
        "icon": "dashboard",
        "order": 1,
        "disabled": false,
        "hidden": false
    }
]

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [1 October 2024 20:13 UTC](https://discourse.nodered.org/t/msg-payload-to-template-node/92110/5 "2024-10-01T20:13:47Z")

</div>

Using sope.$watch may work for you.

```auto
        // Escuchar el mensaje que proviene del nodo inject
        scope.$watch('msg', function(msg) {
            console.log('Mensaje recibido en el template:', msg); // Verificar que se recibe el mensaje

            // Asegurarse de que msg.payload y msg.payload.affectedRows existan
            if (msg.payload && msg.payload.affectedRows !== undefined) {
                // Imprimir el número de affectedRows en la consola
                console.log('Affected Rows:', msg.payload.affectedRows); 
                if (msg.payload.affectedRows !== 0) {
                    showFloatingMessage('Los datos fueron cargados exitosamente.', 'green');
                } else {
                    showFloatingMessage('No se pudieron cargar los datos.', 'red');
                }
            } else {
                // Mensaje de advertencia si no se recibe affectedRows
                showFloatingMessage('Error: affectedRows no definido.', 'red');
            }
        });

```

---

<div class="post-metadata">

### Author: ![LucasPelu](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@LucasPelu](https://discourse.nodered.org/u/LucasPelu)
#### Post date: [1 October 2024 22:13 UTC](https://discourse.nodered.org/t/msg-payload-to-template-node/92110/6 "2024-10-01T22:13:41Z")

</div>

It worked, but every time I deploy the banner appears with the last message I received.I honestly don't understand the reason.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [2 October 2024 17:24 UTC](https://discourse.nodered.org/t/msg-payload-to-template-node/92110/7 "2024-10-02T17:24:15Z")

</div>

The msg.payload is being resent to the template when the page refreshes, you will need to detect a window.onload and halt the script some how.

---

<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 November 2024 17:24 UTC](https://discourse.nodered.org/t/msg-payload-to-template-node/92110/8 "2024-11-01T17:24:30Z")

</div>

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