Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix the issue as: "at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"?

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.

like image 367
Abdu Rëhmãñ Avatar asked Dec 22 '25 09:12

Abdu Rëhmãñ


1 Answers

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.

like image 115
JACKSON MUIRU Avatar answered Dec 23 '25 22:12

JACKSON MUIRU



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!