Javascript 弃用警告:未处理的承诺拒绝已弃用
我的代码中出现了这个错误,我不知道如何修复它Javascript 弃用警告:未处理的承诺拒绝已弃用,javascript,node.js,mongodb,express,Javascript,Node.js,Mongodb,Express,我的代码中出现了这个错误,我不知道如何修复它 DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 到目前为止,我的代码是: router.post('/users/*', (req, r
DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
到目前为止,我的代码是:
router.post('/users/*', (req, res) => {
User.create(new User({
email: req.body.identity.user_email,
authUserId: req.body.identity.user_id
}))
res.json(console.log("User Created"))
})
router.get('/users/:id', (req, res, next) => {
User.findOne({authUserId: req.params.id}, (err, userr) => {
if(err) {
return next(err);
} else if (userr) {
res.json(userr.email);
} else {
res.json(null)
}
});
});
有人能帮我摆脱这个错误吗。
Thnx提前!:) 我猜您的应用程序的入口点是
app.js
。因此,当您将错误
转发为时,返回下一步(err)代码>,应该有人会抓住它并处理它
通常,我将处理程序放在侦听器函数之前的app.js
上-
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
您必须确保捕获承诺拒绝或错误。您没有处理User.create()
可能引发的任何错误。