节点 js 在满足条件时中断并返回到 }).then( data => { 进程



我想知道如何违背承诺,并在满足封信后返回承诺。我提供了下面的代码,并对有问题的部分进行了注释。如您所见,我想要 if 语句,如果满足条件,我想停止该过程,然后返回到承诺,即 }(.then( data => { 部分。

db.employees.find({
  where: {
    id: req.body.employees_id
  },
  include: [{
    model: db.work_sched,
    attributes: ['id', 'code'],
    include: [{
      model: db.work_daysinsched,
      attributes: ['id', 'day'],
      where: {
        day: days[theDay]
      },
      include: [{
        model: db.shiftsched,
        attributes: ['in', 'out', 'shiftType']
      }]
    }],
    required: true
  }]
}).then(data => {
  data.work_sched.work_daysinsched.shiftscheds.forEach(element => {
    var dateInTemp = moment(dateNow + ' ' + element.in).subtract(1, 'h');
    var InHours = '0' + dateInTemp.get('hours');
    var InMinutes = '0' + dateInTemp.get('minutes');
    var InSeconds = '0' + dateInTemp.get('seconds');
    var expectedIn = dateNow + ' ' + (InHours[InHours.length - 2] + InHours[InHours.length - 1]) + ':' + (InMinutes[InMinutes.length - 2] + InMinutes[InMinutes.length - 1]) + ':' + (InSeconds[InSeconds.length - 2] + InSeconds[InSeconds.length - 1]);
    var dateOutTemp = moment(dateNow + ' ' + element.out).add(1, 'h');
    var OutHours = '0' + dateOutTemp.get('hours');
    var OutMinutes = '0' + dateOutTemp.get('minutes');
    var OutSeconds = '0' + dateOutTemp.get('seconds');
    var expectedOut = dateNow + ' ' + (OutHours[OutHours.length - 2] + OutHours[OutHours.length - 1]) + ':' + (OutMinutes[OutMinutes.length - 2] + OutMinutes[OutMinutes.length - 1]) + ':' + (OutSeconds[OutSeconds.length - 2] + OutSeconds[OutSeconds.length - 1]);
    console.log('IN - 1H', expectedIn);
    console.log('OUT + 1H', expectedOut);
    console.log('IN', dateNow + ' ' + element.in);
    console.log('OUT', dateNow + ' ' + element.out);
    let attendance = db.attendance_logs.find({
      where: {
        date: dateNow,
        shiftType: element.shiftType,
        employees_id: req.body.employees_id
      }
    });
    if (attendance == null && moment(DateTime_Now).isBetween(expectedIn, dateNow + ' ' + element.out)) {
      AttendanceService.createAttendance(req, res, dateNow, timeNow, element.shiftType);
      res.send({
        msg: 'Successfully logged in',
        success: true,
        statusCode: 200
      });
      // it the condition is met stop the process the return to promise
      // send response and then end
    } else if (attendance != null && moment(DateTime_Now).isBetween(expectedIn, dateNow + ' ' + element.out)) {

      res.send({
        msg: 'You`re Already Logged in',
        LoggedTime: attendance.dataValues.in,
        success: false,
        statusCode: 500
      });
      // it the condition is met stop the process the return to promise
      // send response and then end
    } else if (attendance != null && attendance.dataValues.out == null && moment(DateTime_Now).isBetween(dateNow + ' ' + element.out, expectedOut)) {

      AttendanceService.logOutInExistingAttendance(req, res, timeNow, attendance.id);
      res.send({
        success: true,
        msg: 'Successfully logged out',
        statusCode: 200
      });
      // it the condition is met stop the process the return to promise
      // send response and then end
    } else if (attendance == null && moment(DateTime_Now).isBetween(dateNow + ' ' + element.out, expectedOut)) {
      AttendanceService.createAttendance2(req, res, dateNow, timeNow, element.shiftType);
      res.send({
        success: true,
        msg: 'Successfully logged out, without sign in.',
        statusCode: 200
      });
      // it the condition is met stop the process the return to promise
      // send response and then end
    }
  });
});

您可以创建标志并在我们有承诺时返回。我建议你需要db.employees.find包装到一个函数中,我们可以在里面调用递归

function findEmployees() {
db.employees.find({
    where: {
        id : req.body.employees_id  
    },
    include: [{
        model:  db.work_sched,
        attributes: ['id', 'code'],
        include : [{
            model : db.work_daysinsched,
            attributes : ['id', 'day'],
            where :{
                day : days[theDay]
            },
            include: [{
                model : db.shiftsched,
                attributes : ['in', 'out', 'shiftType']
            }]
        }],
        required : true
    }]
}).then( data => {
    let promise = null;
     data.work_sched.work_daysinsched.shiftscheds.forEach(element => {
        var dateInTemp = moment(dateNow+' '+element.in).subtract(1,'h');
        var InHours =  '0'+dateInTemp.get('hours')
        var InMinutes =  '0'+dateInTemp.get('minutes')
        var InSeconds =  '0'+dateInTemp.get('seconds')
        var expectedIn = dateNow +' '+(InHours[InHours.length-2]+InHours[InHours.length-1])+':'+(InMinutes[InMinutes.length-2]+InMinutes[InMinutes.length-1])+':'+(InSeconds[InSeconds.length-2]+InSeconds[InSeconds.length-1])
        var dateOutTemp = moment(dateNow+' '+element.out).add(1, 'h');
        var OutHours =  '0'+dateOutTemp.get('hours')
        var OutMinutes =  '0'+dateOutTemp.get('minutes')
        var OutSeconds =  '0'+dateOutTemp.get('seconds')
        var expectedOut = dateNow +' '+(OutHours[OutHours.length-2]+OutHours[OutHours.length-1])+':'+(OutMinutes[OutMinutes.length-2]+OutMinutes[OutMinutes.length-1])+':'+(OutSeconds[OutSeconds.length-2]+OutSeconds[OutSeconds.length-1])
        console.log('IN - 1H', expectedIn)
        console.log('OUT + 1H', expectedOut)
        console.log('IN', dateNow+' '+element.in)
        console.log('OUT', dateNow+' '+element.out)
        let attendance =  db.attendance_logs.find({
            where:{ 
                date : dateNow,
                shiftType : element.shiftType,
                employees_id : req.body.employees_id
            }
        })
        if (promise === null) {
            if(attendance == null && moment(DateTime_Now).isBetween(expectedIn,dateNow+' '+element.out)){
                    promise = AttendanceService.createAttendance(req, res, dateNow, timeNow, element.shiftType)
                        res.send({
                            msg: 'Successfully logged in',
                            success : true,
                            statusCode: 200
                        })
                        // it the condition is met stop the process the return to promise
                    // send response and then end                       
            }else if(attendance != null && moment(DateTime_Now).isBetween(expectedIn,dateNow+' '+element.out)){

                res.send({
                        msg: 'You`re Already Logged in',
                        LoggedTime: attendance.dataValues.in,
                        success : false,
                        statusCode: 500
                    })
                    // it the condition is met stop the process the return to promise
                    // send response and then end      
            }else if(attendance != null && attendance.dataValues.out == null && moment(DateTime_Now).isBetween(dateNow+' '+element.out,expectedOut)){

                promise= AttendanceService.logOutInExistingAttendance(req, res, timeNow, attendance.id)
                        res.send({
                        success : true,
                        msg: 'Successfully logged out',
                        statusCode : 200
                    })
                        // it the condition is met stop the process the return to promise
                    // send response and then end      
            }else if(attendance == null && moment(DateTime_Now).isBetween(dateNow+' '+element.out,expectedOut)){
                promise = AttendanceService.createAttendance2(req, res, dateNow, timeNow, element.shiftType)
                        res.send({
                            success : true,
                            msg: 'Successfully logged out, without sign in.',
                            statusCode : 200
                        })
                      // it the condition is met stop the process the return to promise
                    // send response and then end      
            }
        }
        })
        return promise;
    }).then((data) => {
        findEmployees();
    })
}

最新更新