流星"纤维"从fs.readFile()回调中使用Email.send时出错

时间:2015-09-23 17:00:14

标签: node.js email meteor fs

当我尝试在Error('Can\'t wait without a fiber')的回调中使用Email.send时,我得到fs.readFile。如果我直接拨打Email.send,我就不会收到此错误。

这是错误:

(STDERR) /Users/james/.meteor/packages/meteor-tool/.1.1.8.tvnipv++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:155
  throw new Error('Can\'t wait without a fiber');
        ^
=> Exited with code: 8
(STDERR) Error: Can't wait without a fiber
   at Function.wait (/Users/james/.meteor/packages/meteor-tool/.1.1.8.tvnipv++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:155:9)
   at Object.Future.wait (/Users/james/.meteor/packages/meteor-tool/.1.1.8.tvnipv++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:397:10)
   at smtpSend (packages/email/email.js:86:1)
   at Object.Email.send (packages/email/email.js:176:1)
   at email.js:49:17
   at fs.js:272:14
   at Object.oncomplete (fs.js:108:15)

这是我的JavaScript。请注意,我使用了一个虚拟MAIL_URL来保护无辜者。

if (Meteor.isClient) {
  var to = 'you@example.com'
  var from = 'me@example.com'
  var title = 'Message'
  var message = "emails/message.html"

  Meteor.call(
    'sendEmail'
  , to
  , from
  , title
  , message
  , callback
  )

  function callback(error, data) {
    console.log(error, data)
  }
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    // REPLACE WITH YOUR OWN MAIL_URL FOR OUTGOING MESSAGES
    process.env.MAIL_URL = 'smtp://me%40example.com:PASSWORD@smtp.example.com:25';

    // HACK TO FIND public/ DIRECTORY IN Meteor 1.2.0.1
    var _public = "../../../../../public/"

    var fs = Npm.require('fs');

    Meteor.methods({
      sendEmail: function (to, from, subject, file) {
        var self = this
        var data = file
        check([to, from, subject, file], [String]);

        fs.readFile(_public + file, 'utf8', function (err, data) {
          if (err) {
            console.log('Error: ' + err);
            return;
          }

          // Let other method calls from the same client start,
          // running without waiting for the email sending to
          // complete.
          self.unblock();

          Email.send({ // ERROR OCCURS HERE
            to: to,
            from: from,
            subject: subject,
            html: data
          });
        });
      }
    });
  });
}

如果我通过添加以下注释来绕过fs.readFile的调用,一切正常:

    // fs.readFile(_public + file, 'utf8', function (err, data) {
    //   if (err) {
    //     console.log('Error: ' + err);
    //     return;
    //   }

      // Let other method calls from the same client start,
      // running without waiting for the email sending to
      // complete.
      self.unblock();

      Email.send({ // ERROR HERE
        to: to,
        from: from,
        subject: subject,
        html: data
      });
    // });

您能否帮助我理解为什么在初始案例中需要fiber,以及我应该如何提供?{/ p>

1 个答案:

答案 0 :(得分:1)

Meteor方法调用始终在光纤内部运行,这为Node事件循环回调样式提供了同步查找API。

您可以使用fs.readFile

将异步Meteor.wrapAsync调用转换为同步调用
var fsReadFileSync = Meteor.wrapAsync(fs.readFile, fs);
var data = fsReadFileSync(_public + file, 'utf8');
Email.send(...);

编辑:

  

在内部包装异步读取有什么区别   Meteor.wrapAsync,并使用fs.readFileSync?包裹的异步   阅读导致更好的表现?

fs.readFileSync将阻止Node事件循环,因此仅用于命令行实用程序等事项。

相反,一个被包装的fs.readFile看起来像阻止了事件循环同步执行I / O任务,但是它仍然使用非阻塞回调机制。

在Web服务器应用程序中,您确实不希望您的Node进程被I / O任务阻止,因为这意味着它可能无法尽快回复客户端请求。