I despair of this error.
A function node receives values from a database, edits these, and sends them to a chart.
There are two types of data:
- sum_of_minutes --> the x-axis shows values in HH:MM
- sum_of_days --> the x-axis shall show the day in month (.getDate() )
As long as this function receives "sum_of_minutes", everything is fine. The x-axis of the chart shows times in HH:MM.
But when it comes to "sum_of_days" this thing becomes weird to me since I am not able to find or even UNDERSTAND the bug.
the msg.payload from the database for "sum_of_days" is:
[{"TIME":"1696204799","DAYSUM":0.8},{"TIME":"1696291199","DAYSUM":0.98},{"TIME":"1696377599","DAYSUM":0.36},{"TIME":"1696463999","DAYSUM":0.9},{"TIME":"1696550399","DAYSUM":0.65},{"TIME":"1696636799","DAYSUM":0.96},{"TIME":"1696723199","DAYSUM":0.96},{"TIME":"1696809599","DAYSUM":0.94},{"TIME":"1696895999","DAYSUM":0.73},{"TIME":"1696982399","DAYSUM":0.8},{"TIME":"1697068799","DAYSUM":0.8429575},{"TIME":"1697155199","DAYSUM":0.49678083333333345},{"TIME":"1697241599","DAYSUM":0.4944325000000001},{"TIME":"1697327999","DAYSUM":0.6986259999999999}]
As you can see: "TIME" holds different values (Unix- timestamps) for each field.
The result of the function is this:
{"series":[],"data":[[0.8,0.98,0.36,0.9,0.65,0.96,0.96,0.94,0.73,0.8,0.8429575,0.49678083333333345,0.4944325000000001,0.6986259999999999]],"labels":[20,20,20,20,20,20,20,20,20,20,20,20,20,20]}
As you can see, "labels" holds the same value (20) for each field.
Here is the code of that function:
var data = [];
var times = [];
var arrayLength;
var i;
var dt;
var x_axis = "";
var dummy = [];
if (msg.type_of_data == "sum_of_minutes") {
msg.payload.forEach(function (value) {
data.push(value['MINUTESUM']);
times.push(value['TIME']);
});
}
if (msg.type_of_data == "sum_of_days") {
msg.payload.forEach(function (value) {
data.push(value['DAYSUM']);
times.push(value['TIME']);
});
}
arrayLength = times.length;
for (i = 0; i < arrayLength; i++) {
dt = new Date(Number(times[i]));
if (msg.type_of_data == "sum_of_minutes") {
x_axis = `${dt.getHours().toString().padStart(2, "0")}:${dt.getMinutes().toString().padStart(2, "0")}`;
}
if (msg.type_of_data == "sum_of_days") {
x_axis = dt.getDate();
dummy[i] = times[i] + " ; " + x_axis + " ; " + dt;
}
times[i] = x_axis;
};
msg.payload = [{
series: [],
data: [data],
labels: times
}];
msg.timedata = dummy;
return msg;
Please have a look at this line in the FOR-Loop where I tried some debugging:
dummy[i] = times[i] + " ; " + x_axis + " ; " + dt;
the result of that command is msg.timedata and looks like this:
["1696204799 ; 20 ; Tue Jan 20 1970 16:10:04 GMT+0100 (Mitteleuropäische Normalzeit)","1696291199 ; 20 ; Tue Jan 20 1970 16:11:31 GMT+0100 (Mitteleuropäische Normalzeit)","1696377599 ; 20 ; Tue Jan 20 1970 16:12:57 GMT+0100 (Mitteleuropäische Normalzeit)","1696463999 ; 20 ; Tue Jan 20 1970 16:14:23 GMT+0100 (Mitteleuropäische Normalzeit)","1696550399 ; 20 ; Tue Jan 20 1970 16:15:50 GMT+0100 (Mitteleuropäische Normalzeit)","1696636799 ; 20 ; Tue Jan 20 1970 16:17:16 GMT+0100 (Mitteleuropäische Normalzeit)","1696723199 ; 20 ; Tue Jan 20 1970 16:18:43 GMT+0100 (Mitteleuropäische Normalzeit)","1696809599 ; 20 ; Tue Jan 20 1970 16:20:09 GMT+0100 (Mitteleuropäische Normalzeit)","1696895999 ; 20 ; Tue Jan 20 1970 16:21:35 GMT+0100 (Mitteleuropäische Normalzeit)","1696982399 ; 20 ; Tue Jan 20 1970 16:23:02 GMT+0100 (Mitteleuropäische Normalzeit)","1697068799 ; 20 ; Tue Jan 20 1970 16:24:28 GMT+0100 (Mitteleuropäische Normalzeit)","1697155199 ; 20 ; Tue Jan 20 1970 16:25:55 GMT+0100 (Mitteleuropäische Normalzeit)","1697241599 ; 20 ; Tue Jan 20 1970 16:27:21 GMT+0100 (Mitteleuropäische Normalzeit)","1697327999 ; 20 ; Tue Jan 20 1970 16:28:47 GMT+0100 (Mitteleuropäische Normalzeit)"]
So ... it appears to me that the problem is that:
dt = new Date(Number(times[i]));
since it calculates for each timestamp the same day in 1970 but with different times.
But I have no clue why that happens and why this does not work for "sum_of_days" but for "sum_of_minutes".
Please ... can anyone switch the light on?
Have a great weekend!