Promise await throwing “RangeError: Maximum call stack size exceeded”

Hello, guys I just wanted to ask for the help of this community since I’m not getting help from other communities.
promise awaiting is throwing this error "RangeError: Maximum call stack size exceeded" this is the code on the authController I think its because I’m also awaiting another promise inside the Email class in the sendWelcome method

the repo is this one I have an Email class on utils/email.js

 exports.signup = catchAsync(async (req, res, next) => {
          const newUser = await User.create({
            name: req.body.name,
            email: req.body.email,
            password: req.body.password,
            passwordConfirm: req.body.passwordConfirm
          });
        
          if (!newUser) {
            return next(new AppError('Please complete all fields', 400));
          }
        
          const url = `${req.protocol}://${req.get('host')}/me`;
          console.log(url);
        
          await new Email(newUser, url).sendWelcome();
          
          createSendToken(newUser, 201, res);
        });

the line that has the problem I believe is this one

await new Email(newUser, url).sendWelcome();

This is the class where I use the pug dependency

const nodemailer = require('nodemailer');
const pug = require('pug');
const { convert } = require('html-to-text');

module.exports = class Email {
  constructor(user, url) {
    this.to = user.email;
    this.firstName = user.name.split(' ')[0];
    this.url = url;
    this.from = `Cecilia Benitez <${process.env.EMAIL_FROM}>`;
  }

  newTransport() {
    /*if (process.env.NODE_ENV === 'production') {
      // Sendgrid
      return nodemailer.createTransport({
        service: 'SendGrid',
        auth: {
         user: process.env.SENDGRID_USERNAME,
          pass: process.env.SENDGRID_PASSWORD
        }
      });
    }*/

    return nodemailer.createTransport({
      host: process.env.EMAIL_HOST,
      port: process.env.EMAIL_PORT,
      auth: {
        user: process.env.EMAIL_USERNAME,
        pass: process.env.EMAIL_PASSWORD
      }
    });
  }

  // Send the actual email
  async send(template, subject) {
    // 1) Render HTML based on a pug template
    const html = pug.renderFile(`${__dirname}/../views/email/${template}.pug`, {
      firstName: this.firstName,
      url: this.url,
      subject
    });

    // 2) Define email options
    const mailOptions = {
      from: this.from,
      to: this.to,
      subject,
      html,
      text: convert(html, {
        wordwrap: 130
      })
    };

    // 3) Create a transport and send email
    return await this.newTransport().sendMail(mailOptions);
  }

  async sendWelcome() {
    return await this.send('welcome', 'Welcome to the Natours Family!');
  }

  async sendPasswordReset() {
    return await this.send(
      'passwordReset',
      'Your password reset token (valid for only 10 minutes)'
    );
  }
};

Can you post your question here so that other people can benefit from the question and answer?

What debugging steps have you tried so far? It sounds like you’ve already narrowed it down to one class. I would try commenting out code and see if you can identify the offending lines.

ok, no problem, thanks for answering

If you comment out that code and set const html = "<div>foo</div>" do you still get the error? If so try to comment out other sections of code until you narrow in on the problem.

yes the

const html = "<div>foo</div>"

works just fine, the problem must be where you pointed out, thanks

here is the pug documentation is the same I’m doing

I did npm i pug@latest then deleted node_modules and package.lock.json then npm install and got the same error again

the actual pug template was the problem, sorry about that

1 Like

Glad you figured it out.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.