从transporter.sendMail创建一个observable

时间:2016-08-22 16:07:45

标签: node.js observable nodemailer rxjs5

我正在尝试从sendMail创建一个observable,目前我的测试看起来像那样:

/// <reference path="../../typings/index.d.ts" />
import * as chai from "chai";
let assert = chai.assert;
const Rx = require('rxjs');
var nodemailer = require('nodemailer');

describe("emailPlugin", function() {
   it.only('should emit one value from a callback', function () {
       let mailOptions = {
          from: '"Fred Foo " <MY@gmail.com>', // sender address
          to: 'YOUR@gmail.com', // list of receivers; comma seperated
          subject: 'Hello ✔', // Subject line
          text: 'Hello world ', // plaintext body
          html: '<b>Hello world </b>' // html body
      };
      let transporter = nodemailer.createTransport('smtps://MY%40gmail.com:MY@smtp.gmail.com');

      let boundCallback = Rx.Observable.bindNodeCallback(transporter.sendMail);
      boundCallback(mailOptions, function(error, info){
          if(error){
              return error;
          }
          return info.response;
      }).subscribe(x => console.log(x), e => console.error(e));
  });   
});

结果是错误:

[TypeError: Cannot convert undefined or null to object]

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我和你有完全相同的问题,我发现它解决了这个问题。而不是:

let boundCallback = Rx.Observable.bindNodeCallback(transporter.sendMail);

这对我有用:

let boundCallback = Rx.Observable.bindNodeCallback((options: any, callback: any) => {
  return transporter.sendMail(options, callback);
});

我不知道为什么,在我看来原始形式应该是正确的。看起来TypeScript与sendMail函数的类型相悖。希望有所帮助!