【发布时间】: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