Node-red split msg.payload

36 views Asked by At

Hello i got a problem when i want to split msg.payload Object into 4 separate payloads.

when i run my function i only got the first

Log1

Log2

function node




//
var payload = msg.payload;

// Create separate messages for each key
var msgAddress = { payload: {} };
var msgUtetemp = { payload: {} };
var msgBorvarde = { payload: {} };
var msgTempElement = { payload: {} };

// Loop through the payload keys and assign them to the appropriate message
for (var key in payload) {
    if (payload.hasOwnProperty(key)) {
        if (key.endsWith('_address')) {
            msgAddress.payload[key] = payload[key];
        } else if (key.endsWith('_utetemp')) {
            msgUtetemp.payload[key] = payload[key];
        } else if (key.endsWith('_borvarde')) {
            msgBorvarde.payload[key] = payload[key];
        } else if (key.endsWith('_tempElement')) {
            msgTempElement.payload[key] = payload[key];
        }
    }
}
// Log the separated payloads
console.log("msgAddress:", msgAddress);
console.log("msgUtetemp:", msgUtetemp);
console.log("msgBorvarde:", msgBorvarde);
console.log("msgTempElement:", msgTempElement);


// Return an array containing the separated payloads
return [msgAddress, msgUtetemp, msgBorvarde, msgTempElement]

but when i run code i only get the first msg.payload "msgAddress:" in the message .

msgAdress

debugIm logging complete msg.object,

1

There are 1 answers

0
hardillb On

When returning an array of messages from a function node you need to make it a 2d array to return all the messages from a single output.

e.g.

return [[msgAddress, msgUtetemp, msgBorvarde, msgTempElement]]

This is because the first level of array is the port to output on, and I'm guessing you only have 1 output port configured so all the others are being discarded.

Docs: https://nodered.org/docs/user-guide/writing-functions#multiple-messages