(节点:20732)[DEP0018] DeprecationWarning:未处理的承诺拒绝已被弃用。未来在nodejs中

2024-05-15

我正在尝试在我的应用程序中生成确认链接。虽然它工作正常并且也生成链接,但是当我访问该链接时,它在 Chrome 控制台中显示

POST http://localhost:3000/api/auth/confirmation 400 (Bad Request)

终端给出了这个错误

(node:20732) [DEP0018] DeprecationWarning: Unhandled promise rejections are depr
ecated. In the future, promise rejections that are not handled will terminate th
e Node.js process with a non-zero exit code.
(node:20732) DeprecationWarning: collection.findAndModify is deprecated. Use fin
dOneAndUpdate, findOneAndReplace or findOneAndDelete instead.

这是我的路由器文件 router/auth

router.post('/confirmation', (req, res) => {
  const {token} = req.body.token;
  User.findOneAndUpdate(
    {confirmationToken: token},
    {confirmation: '', confirmed: true},
    {new: true}
).then(user => user ? res.json({user: user.toAuthJSON() }) : res.status(400).json({}));
});

我该如何解决这个问题。这是因为 Promise 处理程序还是 NodeJS 的其他一些问题。


如果您想处理未处理的承诺拒绝问题,您可以致电.catch()关于承诺并处理拒绝。

router.post('/confirmation', (req, res) => {
  const {token} = req.body.token;
  User
  .findOneAndUpdate(
    {confirmationToken: token},
    {confirmation: '', confirmed: true},
    {new: true})
  .then(user => res.json({user: user.toAuthJSON() }))
  .catch(err => res.status(501).send("User- query promise was rejected. Handle according to specific case."));
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

(节点:20732)[DEP0018] DeprecationWarning:未处理的承诺拒绝已被弃用。未来在nodejs中 的相关文章

随机推荐