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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With