I'm trying to expand this function to cover all of our ten staff, but I realise I'm going about it stupidly. Curiously (to me!), the code allows me to copy and paste additional queries only once, i.e. for Dave, but if I copy the code block again, I get "TypeError: Cannot set property 'lazy_count' of undefined". I just lack the programming chops to handle this properly with minimal db queries. Any help much appreciated.
var clockedIn = false;
db.query('SELECT FIRST 1 * FROM ATTENDEVENTS WHERE OPID = 6 ORDER BY DATETIME DESC', function(err, ci_row) {
if (err) throw err;
if (ci_row.length === 1 && ci_row[0].EVENTTYPE === 0) clockedIn = true;
if (!clockedIn) {
db.query('SELECT opid, name FROM OPS WHERE OPID = 6', function(err, rows) {
if (rows.length === 0) {
console.log('error 1')
}
db.query('SELECT FIRST 1 OPID FROM ATTENDEVENTS WHERE OPID = 6 ORDER BY DATETIME DESC', function(err, rows) {
if (err) throw err;
if (rows.length === 1 && rows[0].EVENTTYPE == 0) {
console.log('error 2')
} else {
db.query('SELECT FIRST 1 ATTENDEVENTID FROM ATTENDEVENTS ORDER BY DATETIME DESC', function(err, rows) {
lastID = 0;
if (rows.length === 1) {
lastID = rows[0].ATTENDEVENTID;
}
console.log('George is not clocked in')
});
}
});
});
}
if (clockedIn) {
console.log('George is clocked in')
}
})
var bclockedIn = false;
db.query('SELECT FIRST 1 * FROM ATTENDEVENTS WHERE OPID = 3 ORDER BY DATETIME DESC', function(err, dave_row) {
if (err) throw err;
if (dave_row.length === 1 && dave_row[0].EVENTTYPE === 0) bclockedIn = true;
if (!bclockedIn) {
db.query('SELECT opid, name FROM OPS WHERE OPID = 3', function(err, daverows) {
if (daverows.length === 0) {
console.log('error 1')
}
db.query('SELECT FIRST 1 OPID FROM ATTENDEVENTS WHERE OPID = 3 ORDER BY DATETIME DESC', function(err, daverows) {
if (err) throw err;
if (daverows.length === 1 && daverows[0].EVENTTYPE == 0) {
console.log('error 2')
} else {
db.query('SELECT FIRST 1 ATTENDEVENTID FROM ATTENDEVENTS ORDER BY DATETIME DESC', function(err, daverows) {
lastID = 0;
if (daverows.length === 1) {
lastID = daverows[0].ATTENDEVENTID;
}
console.log('Dave is not clocked in')
});
}
});
});
}
if (bclockedIn) {
console.log('Dave is clocked in')
}
})