Function Arrays from Database

Hello All
I am having a bit of trouble with some arrays within a function node. Currently I have a mysql database that returns the last 7 days worth of data. It returns it all within an array. See here
stats

After it returns the array. I would like to take all wheel rotations and add it together. Simple right? yeah probably for most. I have done it in php but not node red.
Brushing up on my JS in the function I was trying to use the forEach seen below. it does not appear to like this
https://www.w3schools.com/js/js_array_iteration.asp

here is the code I have now which is def wrong, I am used to the array [0] being at the end not the middle and I don't know what to do with it. I need to itterate through it hence the foreach but I am failing obviously

var p0 = msg.payload[0].wheel_rotations ;
p2.forEach(test);
function test(test4)
{
    var test2 = test4 + test2;
}

Any suggestions?

I'm loving jsonata more and more
add this change node after the mysql node and see how it works

[{"id":"35cd9577.751322","type":"change","z":"71c2416a.1852f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sum(payload.wheel_rotations)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":160,"wires":[["8c178f60.66d108"]]}]
1 Like

oh myyyyyyyyyyy god that was so dang easy. I was making it 100x more complicated. I so need to learn more of this jsonata . that is EXACTLY what I need sooo easy.
I will keep this in mind for the rest of my nodes that I will be doing this to and converting some old ones. :slight_smile:

1 Like

It was also easy to do in the query SELECT Sum('wheel_rotations'), (date(now())-Interval 7(day) as diff...........etc

2 Likes