Question about node.send() specifying a message

In a custom node, I only want to output msg information that meets certain conditions. Other msg information that does not meet the conditions should not output any information when passing through the custom node.

        node.on("input", function (msg) {
            let str = msg.payload.toString("hex");
            if(str.slice(2, 4) == "01"){
                msg.operinfo = str.slice(24, 26);
            }
            // else{
            //     msg.payload = null;
            // }
            node.send({ payload: msg.operinfo });
        });

other msg.payload information passing through this node is displayed as undefined. I want undefined information not to be displayed. How do I set this?
Thank you for your reply.

I assume you mean to be NOT displayed in the debug output?

Then you would delete the property.

e.g. delete msg.payload.a_prop_i_dont_want

This is a bad idea. Many users expect the msg to still have properties that they placed in the msg to still be there after traveling through your node - for later use downstream in the flow. In fact, what you have written breaks many things (dashboard, HTTP to name a few - since they EXPECT msg.xyz to be present).

Far better to simply update the property you care about then return the original msg object

e.g.

msg.payload = {} // whatever
node.send(msg)

Thank you for your quick reply. Yes, I do not want to display undefined debug outputs, as shown in the figure.

Clipboard 1

I only want to output "1c", I do not need to output "undefined".

In addition, is delete msg.payload used like this? I tested it and did not get the results I wanted.

        node.on("input", function (msg) {
            let str = msg.payload.toString("hex");
            if(str.slice(2, 4) == "01"){
                msg.payload = str.slice(24, 26);
            }
            else{
               delete msg.payload;
            }
            node.send(msg);
        });

If your problem is that the entire payload is sometimes undefined:

a) Use a switch node after your function to pass only messages whose payload is not null.
or b) Only call node.send(msg) when msg.payload is relevant - inside your if() block.

Thank you very much for your suggestions. I have found a solution:

        node.on("input", function (msg) {
            let str = msg.payload.toString("hex");
            if(str.slice(2, 4) == "01"){
                msg.payload = str.slice(24, 26);
            node.send(msg);
            }
        });

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