Splitting array - from MySQL query

Thanks all,

The two methods high lighted in yellow works.

good stuff.

NOTE - the "hacky" version i posted is identical to the JSON --> JSON version (in that the JSON nodes pretty much do JSON.stringify --> JSON.parse under the hood)

I would recommend the first solution over stringify/parse (if it works)

could you also test....

var rows = msg.payload.map((result) => ({
    ...result,
}));

for (let i=0; i<rows.length; i++) {
    node.send({payload: rows[i]})
}
return null

Yes this works , thank you Sir

Just to be clear, although the output of the split node (after the mysql node) looks like [Object object] when sent to a debug node displaying 'msg.payload', the data is in the msg and can be accessed, the function node is not needed. If you change the debug node to display the 'Complete msg object' you will see all the data.

1 Like

I am on v1.2.9, where do you select 'Complete msg object'?

image

Screen Shot 2021-03-09 at 10.21.28 AM

1 Like

Thank you

... though if the end result is to plot on the worldmap then the function could actually map the property names into new objects at the same time... but ok yeah one step at a time.

2 Likes

That is is what the grand master architecture for the flow is at the end of the day,

So now map msg.payload.tl_lat and msg.payload.tl_lon to msg.payload.lat and msg.payload.lon

And sen_id to name. (And maybe add icon and iconColor. )

1 Like

@ScheepersJohan Johan, just to let you know. there is a new version of node-red-node-mysql (v0,1,2) where the RowDataPacket objects issue has been fixed so you won't see the [Object object] in a debug showing only msg.payload..

1 Like

Thank I will update.

A Function node with this code

let outputMsgs = [];
let parts = msg.payload;
for (var part in parts) {
    outputMsgs.push({payload:parts[part]});
}
return [ outputMsgs ];  
//The [  ] arround the array 'outputMsgs' sends each item in the array as an own message.

@berijan a split node will also split the array into seperate messages.

Yes I know about the Split-node but when browsing through the tread understood it as TS tried the Split without success therefore I suggested to try the Function node.

Yes that was the original problem, but the fix in v0.1.2 of the MySQL node takes care of it.

1 Like

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