mongoose node:internal/errors:464 ErrorCaptureStackTrace(err);错误[ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头

pkmbmrz7  于 2023-06-23  发布在  Go
关注(0)|答案(1)|浏览(280)

所有的函数都在工作,但这个错误来了两个更放或删除函数是一次又一次地调用它的发生的时间

node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:371:5)
    at ServerResponse.setHeader (node:_http_outgoing:576:11)
    at ServerResponse.header (C:\Users\ND\StudioProjects\pos\node_modules\express\lib\response.js:794:10)
    at ServerResponse.send (C:\Users\ND\StudioProjects\pos\node_modules\express\lib\response.js:174:12)
    at ServerResponse.json (C:\Users\ND\StudioProjects\pos\node_modules\express\lib\response.js:278:15)
    at deleteAddItem (file:///C:/Users/ND/StudioProjects/pos/controller/bill/addItem.controller.js:41:21)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 'ERR_HTTP_HEADERS_SENT'
}
const deleteAddItem = async (req, res) => {
  try {
    const getItem = req.body;
    const deleteItem = await AddItem.findOneAndDelete(getItem);
    if (!deleteItem) res.status(203).json({ msg: "No bill available" });
    return res.status(202).json({ msg: "Bill Deleted" });
  } catch (error) {
    console.error(error);
    res.status(500).json({ msg: "Internal Server Error deleteAddItem", error });
  }
};

路由器文件

billRouter.delete("/deleteAddItem", deleteAddItem);

我不知道这是怎么回事!

sz81bmfz

sz81bmfz1#

if配置中添加return语句:

const deleteAddItem = async (req, res) => {
    try {
      const getItem = req.body;
      const deleteItem = await AddItem.findOneAndDelete(getItem);
      if (!deleteItem) return res.status(203).json({ msg: "No bill available" });
      return res.status(202).json({ msg: "Bill Deleted" });
    } catch (error) {
      console.error(error);
      res.status(500).json({ msg: "Internal Server Error deleteAddItem", error });
    }
  };

相关问题