(节点:20732) [DEP0018] 弃用警告:不推荐使用未处理的承诺拒绝.将来在 nodejs 中



我正在尝试在我的应用程序中生成确认链接。 虽然它工作正常并且可以生成链接,但是当我访问该链接时,它在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.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."));
});

最新更新