当我验证令牌并在回调中返回错误时。我尝试再次抛出该错误,但是catch
块无法处理该错误,并且未发送任何响应。请通过修复说明原因!
router.post('/reset-password/verify-token' , (req,res, next) => {
if( !req.body.token || !req.body.userId){
return res.status(400).send({ success: false, message: 'Invalid Url Requested' });
}
User.findOne({ _id: req.body.userId})
.then( user => {
if( !user){
throw new Error('InvalidToken');
}
const secretKey = user.password + user.createdAt.getTime();
jwt.verify(req.body.token, secretKey, (error, result) => {
if (error) {
if( error.name == 'TokenExpiredError') {
throw('TokenExpiredError');
}
throw('Invalid Url Requested');
}
res.status(200).json({ success: true, message: 'Token Verified' });
});
})
.catch(err => {
if(err === 'TokenExpiredError' ){
return res.status(401).json({ success: false, message: 'Link expired please request a new one.' });
}
return res.status(401).json({ success: false, message: err });
});
});