Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
How to sensibly and efficiently expand a function without endless repetition

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')
    }
})
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda