Problem sending an array of objects as message payload

Hi All,
I would like to have a function node sending an array of objects in the message payload'm having a problem getting an array of objects send as the payload of the function below


var charge_data=[];    //Array for message objects
var ChargeTopic="ChargingData"
var TempObject;
var TempMessage; 

for (i = 0; i < msg.payload.energy.total; i++)
    {
    TempObject = 
        {
        StartCharging   : msg.payload.energy.results[i].startDateTime,
        StopCharging    : msg.payload.energy.results[i].endDateTime,
        TimeStamp       : msg.payload.energy.results[i].startDateTime,
        Volume          : msg.payload.energy.results[i].volume
        };

    TempMessage={payload:TempObject,topic:ChargeTopic};
    charge_data.push(TempMessage);
    }
node.warn({ "ChargeData" : charge_data});
    
return [ charge_data ];

It looks like the node sends each TempMessage separately.

15/06/2022, 19:02:34node: 6ab265ae68c42bd7ChargingData : msg.payload : Object
{ StartCharging: "2022-05-25T23:18:08+02:00", StopCharging: "2022-05-26T08:44:00+02:00", TimeStamp: "2022-05-25T23:18:08+02:00", Volume: 56.71 }
15/06/2022, 19:02:34node: 6ab265ae68c42bd7ChargingData : msg.payload : Object
{ StartCharging: "2022-05-24T17:32:10+02:00", StopCharging: "2022-05-25T08:03:20+02:00", TimeStamp: "2022-05-24T17:32:10+02:00", Volume: 20.05 }
15/06/2022, 19:02:34node: 6ab265ae68c42bd7ChargingData : msg.payload : Object
{ StartCharging: "2022-05-23T15:49:45+02:00", StopCharging: "2022-05-24T12:19:46+02:00", TimeStamp: "2022-05-23T15:49:45+02:00", Volume: 47.3 }
15/06/2022, 19:02:34node: 6ab265ae68c42bd7ChargingData : msg.payload : Object
{ StartCharging: "2022-05-19T17:18:06+02:00", StopCharging: "2022-05-20T08:16:05+02:00", TimeStamp: "2022-05-19T17:18:06+02:00", Volume: 28.63 }
15/06/2022, 19:02:34node: 6ab265ae68c42bd7ChargingData : msg.payload : Object
{ StartCharging: "2022-05-19T13:27:54+02:00", StopCharging: "2022-05-19T14:19:19+02:00", TimeStamp: "2022-05-19T13:27:54+02:00", Volume: 9.19 }
15/06/2022, 19:02:34node: 6ab265ae68c42bd7ChargingData : msg.payload : Object
{ StartCharging: "2022-05-18T21:31:54+02:00", StopCharging: "2022-05-19T07:53:24+02:00", TimeStamp: "2022-05-18T21:31:54+02:00", Volume: 35.42 }
15/06/2022, 19:02:34node: 6ab265ae68c42bd7ChargingData : msg.payload : Object
{ StartCharging: "2022-05-16T21:28:21+02:00", StopCharging: "2022-05-17T07:48:48+02:00", TimeStamp: "2022-05-16T21:28:21+02:00", Volume: 14.43 }
15/06/2022, 19:02:34node: 6ab265ae68c42bd7ChargingData : msg.payload : Object
{ StartCharging: "2022-05-15T00:39:44+02:00", StopCharging: "2022-05-15T15:45:53+02:00", TimeStamp: "2022-05-15T00:39:44+02:00", Volume: 44.49 }
15/06/2022, 19:02:34node: 6ab265ae68c42bd7ChargingData : msg.payload : Object
{ StartCharging: "2022-05-13T17:05:48+02:00", StopCharging: "2022-05-14T13:47:58+02:00", TimeStamp: "2022-05-13T17:05:48+02:00", Volume: 31.36 }
15/06/2022, 19:02:34node: 6ab265ae68c42bd7ChargingData : msg.payload : Object
{ StartCharging: "2022-05-12T18:35:33+02:00", StopCharging: "2022-05-13T08:20:25+02:00", TimeStamp: "2022-05-12T18:35:33+02:00", Volume: 51.21 }
15/06/2022, 19:02:34node: 6ab265ae68c42bd7ChargingData : msg.payload : Object
{ StartCharging: "2022-05-08T17:53:35+02:00", StopCharging: "2022-05-09T08:05:21+02:00", TimeStamp: "2022-05-08T17:53:35+02:00", Volume: 18.97 }
15/06/2022, 19:02:34node: 6ab265ae68c42bd7ChargingData : msg.payload : Object
{ StartCharging: "2022-05-05T20:54:24+02:00", StopCharging: "2022-05-06T14:17:50+02:00", TimeStamp: "2022-05-05T20:54:24+02:00", Volume: 36.7 }
15/06/2022, 19:02:34node: 6ab265ae68c42bd7
ChargingData : msg.payload : Object
{ StartCharging: "2022-04-30T17:22:48+02:00", StopCharging: "2022-05-01T14:01:43+02:00", TimeStamp: "2022-04-30T17:22:48+02:00", Volume: 40.35 }

The node.warn however produces the output I want

function : (warn) object
ChargeData: array[13]
[0 … 9]
[10 … 12]

What's wrong?
BR PPee

If you want the array in a payload you need to send it in an object, if you return an array each element of the array is sent separately.
try
return {payload:[ charge_data ]};

This is by design (returning an array indicates to node-red you want to send multiple messages)
It is in the docs.

Instead, set a msg property (e.g. payload) with your data e.g...

var charge_data = [];    //Array for message objects
//... do stuff
//... do stuff
//... do stuff
msg.payload = charge_data     
return msg;

PS, FYI

My advice was

@E1cid advice was

Both are valid however, unless you know what you are doing, the general recommendation is to return the original message object (e.g. return msg; because there may be other properties in the msg needed downstream (e.g. link call nodes, http-nodes, tcp nodes etc)

PS2: the docs: Writing Functions : Node-RED

Got it. Thx all for the swift reply :+1: :+1:

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