Assigning class instance to msg.payload shows up as class name not object in the debug window

Hello :slight_smile: Assigning class instance to msg.payload in a custom decoder node shows up as class name not a nice object you can drill down in the debug window!

Is it normal behaviour as a class instance is also an object although maybe a weird one?

Is it somewhat frustrating since this time I wanted to use classes instead of functions and without the nice drill down object in the debug window it would force me to go back to using functions. Am I the only one having this problem?

What are your advices how to cope with this problem?

I'm using on node-red version on macos BigSur 11.3.1:
19 May 10:08:56 - [info] Node-RED version: v1.3.5
19 May 10:08:56 - [info] Node.js version: v16.1.0

Thanks a lot!

Here is the debug window display:
Screenshot 2021-05-19 at 10.21.24

And here is my simple decoder node with an example of assigning a mock class instance to msg.payload:

module.exports = function (RED) {
    "use strict";
    var osef = require('../osef.js/osef');
    function DecoderNode(config) {
        RED.nodes.createNode(this, config);

        // this class is just here as an example to pinpoint the debug class problem when assigned to msg.payload
        class Measure {
            constructor() {
       = 1;
       = 36947;
        var node = this;
        node.on('input', (msg) => {
            //msg.payload = osef.decode(msg.payload); 
            msg.payload = new Measure();
    RED.nodes.registerType("decoder", DecoderNode);

If you set the debug node to show complete message, you can expand the payload...


I am unsure why that works when the debug set to msg.payload doesnt.

I think it is generally not favoured to send class instances/prototypes in a msg (but I have/do it myself where it makes sense not to deconstruct & reconstruct an object)

Thanks Steve!, I did try that and it works....its not great though from a usage point of view and I will revert to use function objects instead...I did not know that classes where not favoured in node-red nowadays...maybe a warning could be added in the node creation documentation.

I meant passing functions/function objects (not just class instances)

TBH - its early, I think it is fine, where required, to send a class instance (or function object) (I think i was thinking about storing class instances/functions in context) :confused:

Nothing to see here folks :rofl:

No problem thanks a lot Steve to have had a look to this would be nice tough in the future to be able to directly debug class instances in the debug window just like function object :slight_smile:

You can add a .toString method to the prototype to customize the output and make it give whatever details you need. Is it possible to override JavaScript's toString() function to provide meaningful output for debugging? - Stack Overflow

It's not just about context - it can also be between nodes if they use the plugin messaging hooks. In that case all messages may need to be serialisable if those links go remote in any way. Locally it should all work so yes there looks to be something to sort out re payload vs full msg in debug - but you can't assume 100% that it should work with non-serialisable objects.

Hi Kevin and Dcee, excellent suggestion with the toString returning normal objects I just tried it and it works just fine :slight_smile:
Now I got the object I wanted and only a few classes to update:
Thanks a lot guys!

Screenshot 2021-05-19 at 13.02.05

Just added a toString() function to the Measure class:

class Measure {
   constructor() { = 1; = 36947;
   toString() { return { id:, data: } };

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