从已弃用的响应调用更新不起作用

Updating from deprecated response call not working

本文关键字:调用 更新 不起作用 响应      更新时间:2023-09-26

我正在更新已弃用的响应调用,但面临一个不寻常的问题,即响应未结束。通常我会使用:

 res.send(200, {message: 'Location Updated'});

但这现已弃用。我切换到新代码

res.status(200).send(('Location Updated').toString());

但这现在不会返回状态或结束查询。

我正在使用的完整代码

User.findByIdAndUpdate(id, update, {new: true})
  .select('-hash')
  .exec(function(err, user) {
    if (err) return res.sendStatus(422);
    if (!user) return res.sendStatus(401);
    //Update User on Map if active
    if(user.workday == true) { 
      socket.send('location_update', user);
    }
    res.send(200, {message: 'Location Updated'}); // Does end query but is deprecated !!!
    //res.status(200).send(('Location Updated').toString()); //Doesnt end query?!?!?!?
  });

当 res.send 方法结束响应时,它不会退出函数。您必须返回响应才能退出函数并停止查询:

return res.status(200).send('Location Updated');