如何实例化module.exports中声明的类?

时间:2018-01-10 15:32:36

标签: javascript node.js ecmascript-6 nodemailer

我跟随此tutorial发送带有nodemailer和email-template的模板化邮件

typedef void (CALLBACK *FunCallBack)(WCharCP);
void CALLBACK handleEvent(WCharCP unparsed)
{
    elementsExampleCreateShapeActive(unparsed);
}

void attachLibrary()
{
    char* dllName = "qtdialog.dll";
    HMODULE hDLL = LoadLibrary(dllName);
    if (hDLL != NULL)
    {
        typedef int(*pMain)(int, char *[]);
        pMain dialog2 = pMain(GetProcAddress(hDLL, "main"));
        if (dialog2 != NULL)
        {
            dialog2(0, 0);
        }
        typedef void (CALLBACK *PFunCallBack)(FunCallBack);
        PFunCallBack SetFunCallBack = (PFunCallBack)GetProcAddress(hDLL, "SetFunCallBack");
        if (SetFunCallBack)
        {
            SetFunCallBack(handleEvent);
        }
        FreeLibrary(hDLL);
    }
    else
    {
        cout << "Cannot find" << dllName << endl;
    }
}

我不知道如何实例化一个这样声明的类!我总是得到错误

var nodemailer = require('nodemailer');
var emailTemplates = require('email-templates');
var sendMailTransport = require('nodemailer-smtp-transport');

module.exports = {
  _template: null, 
  _transport: null,

  init: function (config) {
    var d = Q.defer();

    emailTemplates(config.emailTplsDir, function (err, template) {
      if (err) {
        return d.reject(err);
      }

      this._template = template;
      this._transport = nodemailer.createTransport({ service: 'Sendgrid', auth: { user: process.env.SENDGRID_USERNAME, pass: process.env.SENDGRID_PASSWORD } });
      return d.resolve();
    }.bind(this));

    return d.promise;
  },

  send: function (from, to, subject, text, html) {
    var d = Q.defer();
    var params = {
      from: from, 
      to: to,
      subject: subject,
      text: text
    };

    if (html) {
      params.html = html;
    }

    this._transport.sendMail(params, function (err, res) {
      if (err) {
        console.error(err);
        return d.reject(err);
      } else {
        return d.resolve(res);
      }
    });

    return d.promise;
  },

  sendMail: function (from, to, subject, tplName, locals) {
    var d = Q.defer();
    var self = this;
    this.init({ emailTplsDir: "email-templates" }).then(function () {
      this._template(tplName, locals, function (err, html, text) {
        if (err) {
          console.error(err);
          return d.reject(err);
        }

        self.send(from, to, subject, text, html)
          .then(function (res) {
            return d.resolve(res);
          });
      });
    }.bind(this));

    return d.promise;
  }
};
  

TypeError:类构造函数如果没有新的

,则无法调用电子邮件

教程中是否存在问题,或者只是我傻了!

0 个答案:

没有答案