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
Add a ment  | 

2 Answers 2

Reset to default 3

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.

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.

本文标签: