Javascript异步函数流程

如果有空位,我的职能部门应该分配一名员工.我不明白为什么即使我使用了等待",程序也不能同步.

My function should assign an employee on a seat if the seat is available. I do not understand why the program doesn't act as synchronous even though I used "await".

在函数的第一行,程序按预期运行.它等待从数据库中获取席位",然后执行if(seats.length > 0)"检查并初始化一个空数组.

In the first lines of the function, the program acts as expected. it waits to get "seats"from the database, then performs the "if(seats.length > 0)" check and initialises an empty array.

async function AssignSeat(req, res) {

  var seats = await connection.SeatEmployees.findAll({
    where: {
      SeatId: req.body.seat.SeatId
    }
  })
  .catch(err => {
    res.status(err.status).json(err)
  });


  if(seats.length > 0){
    var isShared = true;

    var employees = [];

    await seats.forEach(async function(seat){
      var employee = await connection.EmployeesGraph.findAll({
        where: {
          id: seat.EmployeeGraphId
        }
      })
      .catch(err => {
        res.status(err.status).json(err)
      });

      employees.push(employee);
    })
    .catch(err => {
      res.status(err.status).json(err)
    });


    employees.forEach(employee => {
      if(employee.frequent == true)
          isShared = false;
    })


    if(isShared == true){
      //assign user to seat;
    }
  }
}

我的问题在第 13 行代码,在await seat.forEach(async function(seat)"处.

My problem is at the 13th line of code, at " await seats.forEach(async function(seat)".

我要做的是遍历席位"的每个元素,获取分配到该席位的员工,并将其推入员工"数组.

What I want to do is go through each element of "seats", Get the employee assigned to that seat, and push it into the "employees" array.

只有在遍历所有席位并填充员工数组后,我才想继续执行employees.forEach(employee => {"行.

Only after iterating from all the seats and filling the employees array, I want proceed with the "employees.forEach(employee => {" line.

相反,调用之后会发生什么
-----"var employee = await connection.EmployeesGraph.findAll({ "---- ,程序不等待sequelize从数据库中获取员工然后去----"employees.push(employee);"---- ,如预期的那样.
它转到 ----"employees.push(employee);"---- 之后的行上的括号,然后我收到错误 "TypeError: Cannot read property 'catch' of undefined".

Instead, what happens is that after calling
-----"var employee = await connection.EmployeesGraph.findAll({ "---- ,the program doesn't wait for sequelize to get the employee from the database and then go to ----"employees.push(employee);"---- , as intended.
It goes to the paranthesis on the line after ----"employees.push(employee);"---- , then I get the error "TypeError: Cannot read property 'catch' of undefined".

您能解释一下为什么会这样吗?

Could you please explain why this happens?

推荐答案

最简单的解决方案是为此任务使用实际的 for 循环而不是 forEach.forEach() 不会等待迭代所有内容.

The easiest solution is to use an actual for loop instead of forEach for this task. forEach() won't wait to iterate over everything.

try {
  for (const seat of seats) {
    var employee = await connection.EmployeesGraph.findAll({
        where: {
          id: seat.EmployeeGraphId
        }
      })
      .catch(err => {
        res.status(err.status).json(err)
      });

    employees.push(employee);
  }
} catch (err) {
  res.status(err.status).json(err)
}

相关文章