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

时间:2018-08-30 05:47:09

标签: node.js mongodb reactjs

我正在尝试在我的应用程序中生成确认链接。尽管它可以正常工作并生成链接,但是当我访问该链接时,它会在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处理程序或Node.js的其他问题。

1 个答案:

答案 0 :(得分:1)

如果您想压制unhandled promise rejection warning,您要做的就是在诺言上致电.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(400).json({}));
});