希望你的一天顺利,
我有这个代码,用于通过电子邮件链接重置密码,并返回一个带有闪存通知的页面以确认完成:
router.post('/resetPassword', function(req, res, next) {
async.waterfall([
function(done) {
crypto.randomBytes(25, function(err, buf) {
var token = buf.toString('hex');
done(err, token);
});
},
function(token, done) {
User.findOne({ email: req.body.username}, function(err, user) {
if (!user) {
req.flash('error', 'No account with that email address exists.');
return res.redirect('forgot');
}
user.resetPasswordToken = token;
user.resetPasswordExpires = Date.now() + 3600000; // 1 hour
user.save(function(err) {
done(err, token, user);
});
});
},
function(token, user, done) {
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: emailAddress,
pass: config.emailPass
}
});
var mailOptions = {
to: user.email,
from: emailAddress2,
subject: 'Tracker Password Reset',
text: 'A request has been made to update the password on the tracking site.\n\n' +
'Please click on the follow this link to do so:\n\n' +
'http://' + req.headers.host + '/users/reset/' + token + '\n\n' +
'If you did not request this, please ignore this email and your password will remain unchanged.\n'
};
transporter.sendMail(mailOptions, function(err, info){
if (error) {
done(err, 'done');
console.log(error);
} else {
done(err, 'done');
console.log('Email sent: ' + info.response);
}
});
}
], function(err) {
if (err) return next(err);
req.flash('info', 'An e-mail has been sent to ' + user.email + ' with further instructions.');
return res.redirect('back');
});
});
一切正常,将发送电子邮件e.c.t.但它不会像我期望的return res.redirect('back');
一样重定向,它会卡住并最终崩溃。
感谢您的帮助,Ed。
答案 0 :(得分:1)
我认为你放错了你的回报声明。 (并应删除返回)
应该是:
...
, function(err) {
if (err) return next(err);
req.flash('info', 'An e-mail has been sent to ' + user.email + ' with further instructions.');
});
res.redirect('back');
});
答案 1 :(得分:0)
您是否阅读过浏览器控制台以查看是否显示错误?