1

我一般使用 Meteor (1.0.3),但对于一个特殊情况,我使用原始服务器端路由来呈现文件——所以我不在 Meteor 方法之外。

我也在使用 node fs.writeFile/fs.readFile 和 exec 命令来调用 Linux 命令行实用程序。

我提出这个问题的唯一一点是节点调用当然是异步的。所以我选择使用 node Q 库来管理异步回调。

这一切都有效,直到我添加了一行来调用 MongoDB 数据库。

像这样的调用:

var record_name = Mongo_Collection_Name.findOne({_personId: userId}, {fields: {'_id': 0}});

产生以下错误:

[错误:没有光纤就无法等待]

仅当我将函数包装在 Promise 中时才会发生错误。

例如,这样的事情会抛出:

getRecordExample = function () {
  var deferred = Q.defer();
  var record_name = Mongo_Collection_Name.findOne({_personId: userId}, {fields: {'_id': 0}});

  // do something

  // if no error
  deferred.resolve(record_name);

  return deferred.promise;
}

如果我使用 Meteor Fibers 库,我不会收到错误消息:

getRecordExample = function () {
  var deferred = Q.defer();
  Fiber = Npm.require('fibers');
  var record_name

  Fiber(function () {
    record_name = Mongo_Collection_Name.findOne({_personId: userId});
  }).run()
  // do something

  // if no error
  deferred.resolve(record_name);

  return deferred.promise;
}

但是,record_name 变量在光纤之外是未定义的,所以据我所知,我没有办法将变量传递到光纤范围之外。

一个更精确的例子

这有点长,所以你必须向下滚动才能看到所有内容。我基本上在这里建立一个工作流程,所以有流程和子流程。

// both/routes.js
Router.route('/get-route', function(req, res) {
  // get the userId then start the workflow below

  // using Promises here because these were firing concurrently
  Q(userId)
  .then(process_1)
  .then(process_2)
  .done();
}, { name: 'server-side-ir-route', where: 'server' }

// server.js
process_1 = function (userId) {
  sub_process_1(userId);

  sub_process_2(userId);

  return userId;
}

process_2 = function (userId) {
  sub_process_3(userId);

  sub_process_4(userId);

  return userId;
}

sub_process_1 = function (userId) {
  var result = get_record_1(userId);

  // do stuff with result

  // using Q library to call out to async fs.writeFile, return Promise
  fs_writeFile_promise(result)
  .catch(function (error) {
    console.log('error in sub_process_1_write', error);
  })
  .done(function () {
    console.log('done with sub_process_1');
  }

  return userId;
}.future() // <-- if no future() here, the exception is thrown.

sub_process_2 = function (userId) {
  var result = get_record_2(userId);

  // do stuff with result

  // using Q library to call out to async fs.writeFile, return Promise
  fs_writeFile_promise(result)
  .catch(function (error) {
    console.log('error in sub_process_1_write', error);
  })
  .done(function () {
    console.log('done with sub_process_1');
  }

  return userId;
}.future()

// async because of I/O operation (I think)
get_record_1 = function (userId) {
  var record_1 = Mongo_Collection_Name.findOne({'userId': userId});
  // do stuff
  return record_1;
}
get_record_2 = function (userId) {
  var record_2 = Mongo_Collection_Name.findOne({'userId': userId});
  // do stuff
  return record_2;
}

// async operation using Q library to return a Promise
fs_writeFile_promise = function (obj) {
  var deferred = Q.defer();
  fs.writeFile(obj.file, obj.datas, function (err, result) {
    if (err) deferred.reject(err);
    else deferred.resolve('write data completed');
  });
  return deferred.promise;
}

现在,让我们假设 process_2 函数与 process_1 完全相同

此外,我们应该假设我在每个函数中都有 console.log('step_start') 和 console.log('step_end') 。这是它在命令行上的样子:

  • 启动进程
  • 结束进程
  • 启动进程 1
  • 结束进程 1
  • 启动进程 2
  • 启动子进程 1
  • 获得记录 1
  • 启动子进程 2
  • 获得记录 2
  • 返回记录1
  • 结束子进程 1
  • 在子进程 1 中调用 writeData
  • 返回记录2
  • 在子进程 2 中调用 writeData
  • 结束进程 2
  • 结束子进程 1

我必须在 sub_process_1() 函数上放置 Fiber(未来)的原因是,当我将函数 process_1() 放置在 Q 链的顶部时,我得到了错误:没有光纤就无法等待]。

如果我在顶部的 Q 链中删除 process_1() 并从 sub_process_1() 中删除 .future() ,则不会引发异常。

问题

  • 为什么在 Promise 中调用 Mongo 集合会导致 Meteor 应用程序中的光纤错误?
  • 在同步函数中调用异步函数通常会导致同步函数变为异步函数吗?
  • 我该如何解决这个问题?
4

1 回答 1

1

解决这个问题的最常见方法是将使用 Meteor 函数的异步回调包装在Meteor.bindEnvironment().

如果您使用 Meteor 核心WebApp 包来处理您的服务器端路由,代码将是这样的(也在meteorpad中):

WebApp.connectHandlers.use(
  '/test',
  Meteor.bindEnvironment(function(req, res, next) {
    var someSyncData = Players.findOne();
    res.write(JSON.stringify(someSyncData));
    res.end();
  })
);

除非您尝试让多个异步事件同时运行,否则您无需自己使用 Fiber 或 Promise。

为了处理文件读取或其他尚未同步的功能,Meteor 还提供Meteor.wrapAsync()了使它们同步的功能。

还有一些软件包和帮助页面可以为您提供其他高级选项。

于 2015-03-12T05:36:42.210 回答