admin管理员组文章数量:1356451
I want to send a token back to the user to reset his password, but I am getting this error:
Error: there was an error sending the email, try again later
at exports.forgotPassword (C:\\Users\\Abdurehman\\Desktop\\node course\\node practice\\FinalYearProject\\controllers\\authController.js:167:7)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
I searched everywhere, but could not find the solution to my problem, I haven't implemented the resetting functionality yet, because first I want the sending token to work hope ya'll understand.
This is the authController.js
file
Here is my code :
exports.forgotPassword = async (req, res, next) => {
const user = await User.findOne({ email: req.body.email });
if (!user) {
return next(new AppError('no user with this email', 404));
}
const resetToken = user.createPasswordResetToken();
await user.save({ validateBeforeSave: false });
const resetURL = `${req.protocol}://${req.get(
'host'
)}/api/v1/users/resetPassword/${resetToken}`;
const message = `forgot your password ? submit a PATCH request with your new password to : ${resetURL}.\n If you didn't forget your password , please ignore this email`;
try {
await sendEmail({
email: user.email,
subject: 'your password reset token (valid for 10 minutes)',
message,
});
res.status(200).json({
status: 'success',
message: 'Token sent to email',
});
} catch (err) {
user.passwordResetToken = undefined;
user.passwordResetExpires = undefined;
await user.save({ validateBeforeSave: false });
return next(
new AppError('there was an error sending the email , try again later'),
500
);
}
};
Thank you in advance.
I want to send a token back to the user to reset his password, but I am getting this error:
Error: there was an error sending the email, try again later
at exports.forgotPassword (C:\\Users\\Abdurehman\\Desktop\\node course\\node practice\\FinalYearProject\\controllers\\authController.js:167:7)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
I searched everywhere, but could not find the solution to my problem, I haven't implemented the resetting functionality yet, because first I want the sending token to work hope ya'll understand.
This is the authController.js
file
Here is my code :
exports.forgotPassword = async (req, res, next) => {
const user = await User.findOne({ email: req.body.email });
if (!user) {
return next(new AppError('no user with this email', 404));
}
const resetToken = user.createPasswordResetToken();
await user.save({ validateBeforeSave: false });
const resetURL = `${req.protocol}://${req.get(
'host'
)}/api/v1/users/resetPassword/${resetToken}`;
const message = `forgot your password ? submit a PATCH request with your new password to : ${resetURL}.\n If you didn't forget your password , please ignore this email`;
try {
await sendEmail({
email: user.email,
subject: 'your password reset token (valid for 10 minutes)',
message,
});
res.status(200).json({
status: 'success',
message: 'Token sent to email',
});
} catch (err) {
user.passwordResetToken = undefined;
user.passwordResetExpires = undefined;
await user.save({ validateBeforeSave: false });
return next(
new AppError('there was an error sending the email , try again later'),
500
);
}
};
Thank you in advance.
Share Improve this question edited Apr 6, 2024 at 17:44 invzbl3 6,54013 gold badges44 silver badges87 bronze badges asked Jun 9, 2023 at 9:54 Abdu RëhmãñAbdu Rëhmãñ 431 gold badge1 silver badge4 bronze badges 3-
Print the
err
in the catch block. – Lin Du Commented Jun 9, 2023 at 9:57 - i got this from postman when printing the err { "status": "fail", "message": { "errno": -4039, "code": "ESOCKET", "syscall": "connect", "address": "3.209.246.195", "port": 465, "mand": "CONN" } } – Abdu Rëhmãñ Commented Jun 9, 2023 at 10:58
- i am using mailtrap for testing the email – Abdu Rëhmãñ Commented Jun 9, 2023 at 12:06
2 Answers
Reset to default 3I was having the same error and after doing some research I notice that I was missing the "await" keyword before the "transporter.sendMail(message)" inside the sendEmail() function.
I had the same problem while taking the udemy class.
According to the transporter
document, it uses transport.sendMail()
not .sendEmail()
https://nodemailer./usage/#sending-mail
Also, this resolved the problem I had too.
本文标签:
版权声明:本文标题:javascript - How to fix the issue as: "at process.processTicksAndRejections (node:internalprocesstask_queues:95:5)& 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744057459a2583513.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论