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

时间:2018-04-20 02:18:15

标签: node.js promise conditional-statements break

我想知道如何打破承诺并在满足后得到它。我提供了下面的代码,并对有问题的部分发表评论。正如你所看到的那样,我想要if语句如果满足条件我想停止进程然后返回到承诺,那就是})。then(data => {part。

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
    }
  });
});

1 个答案:

答案 0 :(得分:0)

您可以创建标志并在我们承诺时返回。我建议你需要将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();
    })
}