Storing an object into the flow

Hi all,

actually, storing an object into the flow drives me crazy.
If I process the code below, I got always the String "[object Object]" stored in the flow and even if I return it to the debug it is the same string returned.

I assume I pass somewhere the object to a string but where?

/* Definition of classConsumptions */
function classConsumptions() {
    this.valuePairs = [];
    
    function classPair(key, value) {
        this.key = key;
        this. value = value;
    }
    
    this.addValue = function(key, value) {
        /* Check if key exists */
        var indexKey = this.valuePairs.findIndex(i => i.key === key)
        
        switch (indexKey) {
            case -1:
                this.valuePairs.push(new classPair(key, value));
                break;
            default:
                this.valuePairs[indexKey].value = value;
                break;
        }
    }
    
    this.printComsumptionsToDebug = function(){
        this.valuePairs.forEach(function(element) {
            node.warn("Key: " + element.key + " Value: " + element.value);
        })
    }
}


/* Main */
var localConsumptions = flow.get("consumptions");
localConsumptions = ( typeof localConsumptions != "undefined" && localConsumptions instanceof Object ) ? localConsumptions  : new classConsumptions();

localConsumptions.addValue(msg.topic, msg.payload);

flow.set("consumptions", localConsumptions);

return localConsumptions;

Thanks for your help.

What do you see if you change your debug node to show complete object?

Hi cymplecy,

actually exactly the same:
Bild1

Best

You should really only be storing JSON encodable values in context and not custom objects. If you were to use anything but the Memory context store, then your code would not work.

The probably is likely the fact you are returning your custom object at the end and not a message object.

@knolleary: thanks for the feedback and the short explanation. Was expecting something like this.

I changed the code mostly to JSON (at least I think so) and it works now. Unfortunately I was not able find a way to have a nested function/method as part of the JSON data object. Is this or something similar possible, or I need to rewrite in every functions the required methods operating on the data again?

Have a good evening.

New code:

/* JSON Dataformat */
consumptions = {
    "date": false,
    valuePairs: []
}

/* Main */
var localConsumptions = flow.get("consumptions");
localConsumptions = ( typeof localConsumptions != "undefined" && localConsumptions instanceof Object ) ? localConsumptions : consumptions;

var key = msg.topic;
var value = msg.payload;

switch (key) {
    case "date":
        localConsumptions.date = msg.payload;
        break;
    default:
        var indexKey = localConsumptions.valuePairs.findIndex(i => i.key === key)
        switch (indexKey) {
            case -1:
                localConsumptions.valuePairs.push( {"key": key, "value": value} );
                break;
            default:
                localConsumptions.valuePairs[indexKey].value = value;
                break;
        }
        break;
}

flow.set("consumptions", localConsumptions);

return localConsumptions;