【问题标题】:promise await throwing "RangeError: Maximum call stack size exceeded"承诺等待抛出“RangeError:超出最大调用堆栈大小”
【发布时间】:2021-07-02 00:52:47
【问题描述】:

promise awaiting 抛出这个错误"RangeError: Maximum call stack size exceeded"

repo 是 one 我在 utils/email.js

上有一个 Email 类

这是我使用 pug 依赖的类 如果我只在 h1 中发送一个字符串,请查看 pug.renderFile,它可以正常工作,但使用 pug 则不能,我做错了什么,pug 文档就像我的代码一样,请提前帮助和感谢。

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)'
    );
  }
};

【问题讨论】:

  • 您似乎正在尝试对包含循环引用的对象进行字符串化/渲染。不要猜测问题出在哪里,查看堆栈跟踪。
  • 我没有在那个方法中使用 JSON.stringify 如果这就是你的意思,我只是传递用户对象。
  • 不管怎样,堆栈跟踪说明了有关 node_modules 的一些信息,这不是我的代码,那该怎么办?

标签: javascript node.js async-await pug


【解决方案1】:

转到 views/email/baseEmail.pug 并把它改为

//- Email template adapted from https://github.com/leemunroe/responsive-html-email-template
//- Converted from HTML using https://html2pug.now.sh/
doctype html
html
  head
    meta(name='viewport', content='width=device-width')
    meta(http-equiv='Content-Type', content='text/html; charset=UTF-8')
    title= subject

    include _style
  body
    table.body(role='presentation', border='0', cellpadding='0', cellspacing='0')
      tbody
        tr
          td
          td.container
            .content
              // START CENTERED WHITE CONTAINER
              table.main(role='presentation')

                // START MAIN AREA
                tbody
                  tr
                    td.wrapper
                      table(role='presentation', border='0', cellpadding='0', cellspacing='0')
                        tbody
                          tr
                            td
                              // CONTENT
                              block content

              // START FOOTER
              .footer
                table(role='presentation', border='0', cellpadding='0', cellspacing='0')
                  tbody
                    tr
                      td.content-block
                        span.apple-link Natours Inc, 123 Nowhere Road, San Francisco CA 99999
                        br
                        |  Don't like these emails? 
                        a(href='#') Unsubscribe
          //- td 

【讨论】:

    猜你喜欢
    • 2018-01-24
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 2018-03-02
    • 2021-05-30
    • 2021-07-11
    相关资源
    最近更新 更多