0

.I use callback to use variables after their values are updated. But the variables values are not updated. I can't find why. Do you have suggestions so that I can make my code synchronize and return the updated variables at last. Here is my code service layer

exports.getPeopleSummary =  (req, res, callback) => {

    req.query.limit = 1000
    req.query.offset = 0

    var headCount=0;
    var inCount=0;
    var outCount=0;
    var leaveCount=0;
    var absentCount=0;

    /////////head count
    emploployeeService.findAll(req,res, (employees)=>{
      headCount = employees.length
      console.log("Head",headCount)
    })

    //////in/out count
    
    let ts = Date.now();

    let date_ob = new Date(ts);
    let date = date_ob.getDate();
    let month = date_ob.getMonth() + 1;
    let year = date_ob.getFullYear();

    // prints date & time in YYYY-MM-DD format
    const today = year + "-" + month + "-" + date+"T08:30:00.000";
    const inOutCountSql = "SELECT fprints from attendence_tbl where date = ?"
    const inOutCountValues = [today]

    mysqlConnection.query(inOutCountSql,inOutCountValues,(err, records, fields) => {
      if (!err) {
        records.forEach(record=>{
        if(Array.isArray(record)){ //if record is an array
          if("in"==record[record.length-1].direction){
            inCount++
          }else if ("out"==record[record.length-1].direction){
            outCount++
          }
        }else{
          if("in"==record.direction){
            inCount++
          }else if ("out"==record.direction){
            outCount++
          }
        }
        })
      }
    
      else res.send(err);
    })

    /////leave count
    const leaveCountSql = "SELECT emp_no from leaves_tbl where leaveDate = ?"
    const leaveCountValues = [today]

    mysqlConnection.query(leaveCountSql,leaveCountValues,(err, leaves, fields) => {
      if (!err) {
        leaveCount=leaves.length
      }
    })
    absentCount = headCount-leaveCount
    callback(headCount, inCount, outCount, leaveCount, absentCount)
}

Here at controller layer

    exports.peopleSummary = async (req, res) => {
        try {analyticsService.getPeopleSummary(req, res, (headCount, inCount, outCount, leaveCount, absentCount)=>{
      res.send({"total_head_count:":headCount ,"in_count: ":inCount,"out_count: ":outCount, "leave_count: ":leaveCount, "absent_count: ":absentCount})
    });
        } catch (err) {
    console.log(err);}};

This outputs in console Head 307 because it has 307 employee entries

but the response in postman is

{
    "total_head_count:": 0,
    "in_count: ": 0,
    "out_count: ": 0,
    "leave_count: ": 0,
    "absent_count: ": 0
}
evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
  • 1
    You assume that the function passed in to mysql.query (third parameter) has already executed once you get to the line of code following mysql.query, but it hasn't. That function only runs when data has been received. – James Sep 26 '22 at 11:40
  • Yes. can you suggest me a method to make sure that function is completed and then continue with values updated – user13396345 Sep 27 '22 at 09:02

0 Answers0