Skip to content

Commit 42c6ac5

Browse files
jaylagorioscottleibrand
authored andcommitted
Guard bg[i] from being null in render BG graph (#15)
* Guard bg[i] from being null in render BG graph I was getting this error during looping and it would stop the loop: ``` /root/src/openaps-menu/scripts/status.js:103 var x = 2 + zero_x + Math.round(((((bg[i].date - zerotime)/1000)/60)/5)); ^ TypeError: Cannot read property 'date' of undefined at Object.<anonymous> (/root/src/openaps-menu/scripts/status.js:103:47) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Function.Module.runMain (module.js:693:10) at startup (bootstrap_node.js:188:16) at bootstrap_node.js:609:3 ``` This led me to believe that one of the items in the array is null, so this skips that loop iteration. After doing this the loop was running properly again. * Update status.js
1 parent cb51bef commit 42c6ac5

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

scripts/status.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,21 @@ var numBGs = (suggested.predBGs != undefined) ? (72) : (120); //fill the whole g
9999
var date = new Date();
100100
var zerotime = date.getTime() - ((numBGs * 5) * 600);
101101
var zero_x = numBGs + 5;
102-
for (var i = 0; i <= numBGs; i++) {
103-
var x = 2 + zero_x + Math.round(((((bg[i].date - zerotime)/1000)/60)/5));
104-
var y = Math.round( 21 - ( ( bg[i].glucose - 250 ) / 8 ) );
105-
//left and right boundaries
106-
if ( x < 5 ) x = 5;
107-
if ( x > 127 ) x = 127;
108-
//upper and lower boundaries
109-
if ( y < 21 ) y = 21;
110-
if ( y > 51 ) y = 51;
111-
display.oled.drawPixel([x, y, 1]);
112-
// if we have multiple data points within 3m, look further back to fill in the graph
113-
if ( bg[i-1] && bg[i-1].date - bg[i].date < 200000 ) {
114-
numBGs++;
102+
for (var i = 0; i < numBGs; i++) {
103+
if (bg[i] != null) {
104+
var x = 2 + zero_x + Math.round(((((bg[i].date - zerotime)/1000)/60)/5));
105+
var y = Math.round( 21 - ( ( bg[i].glucose - 250 ) / 8 ) );
106+
//left and right boundaries
107+
if ( x < 5 ) x = 5;
108+
if ( x > 127 ) x = 127;
109+
//upper and lower boundaries
110+
if ( y < 21 ) y = 21;
111+
if ( y > 51 ) y = 51;
112+
display.oled.drawPixel([x, y, 1]);
113+
// if we have multiple data points within 3m, look further back to fill in the graph
114+
if ( bg[i-1] && bg[i-1].date - bg[i].date < 200000 ) {
115+
numBGs++;
116+
}
115117
}
116118
}
117119

0 commit comments

Comments
 (0)