Unset property overwrites text node

Hi,
I'm using node red since more than 4 years and i have a question regarding the text node.
When I am using the mustache syntax to set the value format to e.g.: "{{msg.payload.second}}"
everything works fine, if the incoming messages has the property "msg.payload.second" set.

Bur when the incoming messages has NO property "msg.payload.second" the text field ist cleared.

I expected, that if the value is missing, the last value will still be displayed.

I am asking this because I have some function nodes, which will be feed with messages from different topics. The function does the same operation on the payload and than it returns the payload based on the topic. For each topic I have a text node to display the result. But all other results are deleted because they are not set by the function node.

I created a small example to show what i mean.
I am new here and i can't upload code, so you'll find it here:
Example

If there is no relevant data then why not return null instead. Ie don’t return anything

You dont need to. You can export your small demo flow to clipboard and then paste it into a reply after pressing the </> code toolbar button (or surround the flow with triple ``` backticks on new lines e.g

```
< paste flow here >
```

That's what I am trying to do. But I want to connect multiple text nodes and selectively feed data into few of them and the once which receive the NULL value should not change there data.

My workaround is to configure the function node to have 4 outputs and selectively send data on the relevant outputs. But this workaround is quite complicated when dealing more than 5 Outputs,

Here my example:
upper part the functionality I would like to have (and don't know how to implement it in detail)
lower part my workaround.

[
    {
        "id": "e4fd59d6388ce474",
        "type": "tab",
        "label": "EmptyText",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "95b5b1dca3a6b5f6",
        "type": "inject",
        "z": "e4fd59d6388ce474",
        "name": "first and second",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "12",
        "payload": "",
        "payloadType": "date",
        "x": 140,
        "y": 400,
        "wires": [
            [
                "935c46bee3d83810",
                "8a30dccddc6edfdf"
            ]
        ]
    },
    {
        "id": "935c46bee3d83810",
        "type": "function",
        "z": "e4fd59d6388ce474",
        "name": "Set Two Properties",
        "func": "var myMsg = {};\nmyMsg.payload = {};\nmyMsg.topic = msg.topic;\n\nif (msg.topic.includes(\"1\")) {\n    myMsg.payload.first = msg.payload;\n}\n\nif (msg.topic.includes(\"2\")) {\n    myMsg.payload.second = msg.payload;\n}\n\nif (msg.topic.includes(\"3\")) {\n    myMsg.payload.third = msg.payload;\n}\n\nreturn myMsg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 490,
        "y": 220,
        "wires": [
            [
                "4cbe53432e434550",
                "2d873a5f37ee7060",
                "e3d03199a3da2725",
                "f193466098d68e54"
            ]
        ]
    },
    {
        "id": "4cbe53432e434550",
        "type": "debug",
        "z": "e4fd59d6388ce474",
        "name": "debug",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 710,
        "y": 300,
        "wires": []
    },
    {
        "id": "9f225f850a0f5355",
        "type": "inject",
        "z": "e4fd59d6388ce474",
        "name": "first",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "1",
        "payload": "",
        "payloadType": "date",
        "x": 170,
        "y": 240,
        "wires": [
            [
                "935c46bee3d83810",
                "8a30dccddc6edfdf"
            ]
        ]
    },
    {
        "id": "c243adf09777690e",
        "type": "inject",
        "z": "e4fd59d6388ce474",
        "name": "second",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "2",
        "payload": "",
        "payloadType": "date",
        "x": 170,
        "y": 280,
        "wires": [
            [
                "935c46bee3d83810",
                "8a30dccddc6edfdf"
            ]
        ]
    },
    {
        "id": "2d873a5f37ee7060",
        "type": "ui_text",
        "z": "e4fd59d6388ce474",
        "group": "b7d12b8d.651c8",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "first",
        "label": "first",
        "format": "{{msg.payload.first}}",
        "layout": "row-spread",
        "className": "",
        "x": 710,
        "y": 180,
        "wires": []
    },
    {
        "id": "e3d03199a3da2725",
        "type": "ui_text",
        "z": "e4fd59d6388ce474",
        "group": "b7d12b8d.651c8",
        "order": 2,
        "width": 0,
        "height": 0,
        "name": "second",
        "label": "second",
        "format": "{{msg.payload.second}}",
        "layout": "row-spread",
        "className": "",
        "x": 720,
        "y": 220,
        "wires": []
    },
    {
        "id": "8a30dccddc6edfdf",
        "type": "function",
        "z": "e4fd59d6388ce474",
        "name": "Set Two Properties",
        "func": "var myMsg1 = {};\nvar myMsg2 = {};\nvar myMsg3 = {};\nvar mask = 0;\nmyMsg1.payload = [];\nmyMsg2.payload = [];\nmyMsg3.payload = [];\n\nif (msg.topic.includes(\"1\")) {\n    myMsg1.payload = msg.payload;\n    mask += 1;\n}\n\nif (msg.topic.includes(\"2\")) {\n    myMsg2.payload = msg.payload;\n    mask += 2;\n}\n\nif (msg.topic.includes(\"3\")) {\n    myMsg3.payload = msg.payload;\n    mask += 4;\n}\n\nswitch (mask) {\n    case 1: return [myMsg1,  [],     []];\n    case 2: return [[],      myMsg2, []];\n    case 3: return [myMsg1,  myMsg2, []];\n    case 4: return [[],      [],     myMsg3];\n    case 5: return [myMsg1,  [],     myMsg3];\n    case 6: return [[],      myMsg2, myMsg3];\n    case 7: return [[],      myMsg2, myMsg3];\n}\n",
        "outputs": 3,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 490,
        "y": 540,
        "wires": [
            [
                "2eecfa5ec406ec0e",
                "35b39ebb6ca08a21"
            ],
            [
                "e82eccbb21ac25e6",
                "1184441045b1f11c"
            ],
            [
                "81a888158d3a0ddf",
                "415307a74a70f98a"
            ]
        ]
    },
    {
        "id": "2eecfa5ec406ec0e",
        "type": "ui_text",
        "z": "e4fd59d6388ce474",
        "group": "09b1797e1715fc63",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "first",
        "label": "first",
        "format": "{{msg.payload}}",
        "layout": "row-spread",
        "className": "",
        "x": 710,
        "y": 440,
        "wires": []
    },
    {
        "id": "e82eccbb21ac25e6",
        "type": "ui_text",
        "z": "e4fd59d6388ce474",
        "group": "09b1797e1715fc63",
        "order": 2,
        "width": 0,
        "height": 0,
        "name": "second",
        "label": "second",
        "format": "{{msg.payload}}",
        "layout": "row-spread",
        "className": "",
        "x": 720,
        "y": 540,
        "wires": []
    },
    {
        "id": "6182837352813bfa",
        "type": "comment",
        "z": "e4fd59d6388ce474",
        "name": "What I like to have",
        "info": "",
        "x": 490,
        "y": 180,
        "wires": []
    },
    {
        "id": "179c6f3276b38d9d",
        "type": "comment",
        "z": "e4fd59d6388ce474",
        "name": "My Workarround",
        "info": "",
        "x": 480,
        "y": 480,
        "wires": []
    },
    {
        "id": "230ba99712228c90",
        "type": "inject",
        "z": "e4fd59d6388ce474",
        "name": "third",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "3",
        "payload": "",
        "payloadType": "date",
        "x": 170,
        "y": 320,
        "wires": [
            [
                "935c46bee3d83810",
                "8a30dccddc6edfdf"
            ]
        ]
    },
    {
        "id": "f193466098d68e54",
        "type": "ui_text",
        "z": "e4fd59d6388ce474",
        "group": "b7d12b8d.651c8",
        "order": 2,
        "width": 0,
        "height": 0,
        "name": "third",
        "label": "third",
        "format": "{{msg.payload.third}}",
        "layout": "row-spread",
        "className": "",
        "x": 710,
        "y": 260,
        "wires": []
    },
    {
        "id": "ef7c34cae1c0c29f",
        "type": "inject",
        "z": "e4fd59d6388ce474",
        "name": "second and third",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "23",
        "payload": "",
        "payloadType": "date",
        "x": 140,
        "y": 440,
        "wires": [
            [
                "8a30dccddc6edfdf",
                "935c46bee3d83810"
            ]
        ]
    },
    {
        "id": "6a1deee32e832f6d",
        "type": "inject",
        "z": "e4fd59d6388ce474",
        "name": "all",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "123",
        "payload": "",
        "payloadType": "date",
        "x": 170,
        "y": 480,
        "wires": [
            [
                "935c46bee3d83810",
                "8a30dccddc6edfdf"
            ]
        ]
    },
    {
        "id": "81a888158d3a0ddf",
        "type": "ui_text",
        "z": "e4fd59d6388ce474",
        "group": "09b1797e1715fc63",
        "order": 2,
        "width": 0,
        "height": 0,
        "name": "third",
        "label": "third",
        "format": "{{msg.payload}}",
        "layout": "row-spread",
        "className": "",
        "x": 710,
        "y": 640,
        "wires": []
    },
    {
        "id": "35b39ebb6ca08a21",
        "type": "debug",
        "z": "e4fd59d6388ce474",
        "name": "debug1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 720,
        "y": 480,
        "wires": []
    },
    {
        "id": "1184441045b1f11c",
        "type": "debug",
        "z": "e4fd59d6388ce474",
        "name": "debug2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 720,
        "y": 580,
        "wires": []
    },
    {
        "id": "415307a74a70f98a",
        "type": "debug",
        "z": "e4fd59d6388ce474",
        "name": "debug3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 720,
        "y": 680,
        "wires": []
    },
    {
        "id": "b7d12b8d.651c8",
        "type": "ui_group",
        "name": "Example",
        "tab": "fe5d396a.0d6d4",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "09b1797e1715fc63",
        "type": "ui_group",
        "name": "Workarround",
        "tab": "fe5d396a.0d6d4",
        "order": 2,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "fe5d396a.0d6d4",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

You would need to store the values, then recall the values and overwrite new values.
e.g.

const  myMsg = {};
myMsg.payload = context.get("store") || {}; // retreive stored payload
const topic = msg.topic.split(""); // create array of topic indexes for textnames
const text_names = ["first", "second", "third"]; // payload property names

// loop through topic array values
topic.forEach(str =>  { 
    let index = Number(str) -1; // convert to number and correct for index
    let property = text_names[index]; // set property name with index
    if(property){ //check property is defined
        myMsg.payload[property] = msg.payload; // set payload to correct property
    }
})
context.set("store", myMsg.payload); // store payload
return myMsg;

GREAT

I have learned so much from your answer!

I thought, that i had to store the state in the context
but i was nor aware, that i can store a whole object
including all properties at once.

I'm a embedded programmer using C,
not so much involved in javascript.

So THANKS a lot for this marvelous Answer.

When you are in the Ruhr-Region in Germany
come around and I offer you a beer and Currywurst

the typical meal in this region.

Dario.

2 Likes

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