簡體   English   中英

當我嘗試處理 mongoose.findOne 中的錯誤時,它給了我“未處理的‘錯誤’事件”

[英]When I try to handle error in mongoose.findOne it give me `Unhandled 'error' event`

Mongoose 未處理的“錯誤”事件

當我嘗試處理mongoose.findOne()函數中的錯誤時,我想使用像這樣的第二個參數mongoose.findOne({ uuid: uuid }, (err,doc))但由於以下錯誤而看不到任何錯誤:

扔er; // 未處理的“錯誤”事件

這是我的用法:

  await endPointSchema.findOne({ uuid: uuid }, (err, doc) => {
    if (err) {
      res.status(403).json({ message: "Something went wrong!" });
      return;
    }
    
    if (!doc.endpoint) {
      const data = new endPointSchema({
        uuid: uuid,
        endpoint: [{ point: Endpoint, Content }],
        date: Date.now(),
      });
      data.save();
      res.status(200).json({ message: "Succesfully Created new Breakpoint" });
      return;
    }

    doc.endpoint = [...doc.endpoint, {Endpoint: Content}]
    doc.save()
    res.status(200).json({ message: "Succesfully Created new Breakpoint" });
    return
  });

第一次解決后

現在我收到這個錯誤;

(node:8140) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:561:11) at ServerResponse.header (C:\\Users\\admin\\Desktop\\Nedim Akar\\project-a server\\node_modules\\express\\lib\\response.js:771:10) at ServerResponse.send (C:\\Users\\admin\\Desktop\\Nedim Akar\\project-a server\\node_modules\\express\\lib\\response.js:170:12)

這是我的終點;

route.post("/endpoint", AuthorizePanel, async (req, res) => {
  const { role, username, uuid } = req.user;
  if (!role && !username && !uuid) {
    res.sendStatus(401);
    return;
  }

  const { Endpoint, Content } = req.body;
  if (Endpoint === username) {
    res.status(403).json({ message: "Endpoint can not be same with your username!" });
    return;
  }

 try {
const doc = await endPointSchema.findOne({ uuid: uuid })
    if (!doc.endpoint) {
      const data = new endPointSchema({
        uuid: uuid,
        endpoint: [{ point: Endpoint, Content }],
        date: Date.now(),
      });
      await data.save();
      res.status(200).json({ message: "Succesfully Created new Breakpoint" });
      return;
    }

    doc.endpoint = [...doc.endpoint, {Endpoint: Content}]
    await doc.save()
    res.status(200).json({ message: "Succesfully Created new Breakpoint" });
    return
} catch(e) {
      res.status(403).json({ message: "Something went wrong!" });
}
});

我用什么?

  • 快遞: ^4.17.1
  • 貓鼬: ^5.12.14

您不應同時使用 async/await 和回調。 這是你應該如何重構代碼:

try {
const doc = await endPointSchema.findOne({ uuid: uuid })
    if (!doc.endpoint) {
      const data = new endPointSchema({
        uuid: uuid,
        endpoint: [{ point: Endpoint, Content }],
        date: Date.now(),
      });
      await data.save();
      res.status(200).json({ message: "Succesfully Created new Breakpoint" });
      return;
    }

    doc.endpoint = [...doc.endpoint, {Endpoint: Content}]
    await doc.save()
    res.status(200).json({ message: "Succesfully Created new Breakpoint" });
    return
} catch(e) {
      res.status(403).json({ message: "Something went wrong!" });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM