【问题标题】:nodemailer is returning is not a constructor + unhandled promise rejectionnodemailer正在返回不是构造函数+未处理的承诺拒绝
【发布时间】:2020-08-23 09:35:09
【问题描述】:

我有一个主 index.js 并调用 nodemailer

  new aaaaa.send(`${userEmail}`)

这是我的 nodemailer.js

const nodemailer = require('nodemailer');
const EmailTemplate = require('email-templates');

const {
  nodemailer_client_secret,
  nodemailer_refresh_token,
  nodemailer_access_token,
  nodemailer_user,
  nodemailer_client_id,
  sp_auth,
} = require('../../config/config');
const { gmail } = require('googleapis/build/src/apis/gmail');

class aaaaa {
  // create reusable transporter object using the default SMTP transport
  static async send(userEmail, blackListRandom) {

    const emailVerify = await nodemailer.createTransport({
      host: 'smtp.gmail.com',
      port: 465,
      secure: true,
      auth: {
        type: 'OAuth2',
        user: nodemailer_user,
        clientId: nodemailer_client_id,
        clientSecret: nodemailer_client_secret,
        refreshToken: nodemailer_refresh_token,
        accessToken: nodemailer_access_token,
        expires: 3600,
      },
    });

    const email = new EmailTemplate({
      transport: emailVerify,
      send: true,
      preview: false,
    });
    email
      .send({
        template: 'hiii',
        message: {
          from: '',
          to: userEmail,
        },
        locals: {
        },
      })
      .then(() => console.log('email has been sent!'))
  }
}

module.exports = aaaaa;

我收到来自这封邮件的电子邮件。但是,我怎样才能摆脱我得到的两个警告?

  • UnhandlePromiseRejectionWarning:TypeError 不是构造函数
  • 解除承诺拒绝?

【问题讨论】:

  • 这意味着您应该为send 函数添加try/catchthen/catch 或在应用程序的顶层添加UnhandlePromiseRejection 事件监听器

标签: node.js express nodemailer


【解决方案1】:

我建议删除 new 关键字或将其拆分为新行,因为它正在应用于导致错误的静态方法。

const emailObj = new aaaaa();
emailObj.send(`${userEmail}`);

【讨论】:

  • 这是一个静态函数,所以根本不需要新的
  • 有什么理由不触发错误?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-08
相关资源
最近更新 更多