首页 文章

错误:流星代码必须始终在光纤内运行

提问于
浏览
16

我在我的应用程序中使用条带付款,我想在成功交易后在我自己的数据库中创建收据文档

我的代码:

Meteor.methods({
  makePurchase: function(tabId, token) {
    check(tabId, String);
    tab = Tabs.findOne(tabId);

    Stripe.charges.create({
      amount: tab.price,
      currency: "USD",
      card: token.id
    }, function (error, result) {
      console.log(result);
      if (error) {
        console.log('makePurchaseError: ' + error);
        return error;
      }

      Purchases.insert({
        sellerId: tab.userId,
        tabId: tab._id,
        price: tab.price
      }, function(error, result) {
        if (error) {
          console.log('InsertionError: ' + error);
          return error;
        }
      });
    });
  }
});

但是,此代码返回错误:

Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.

我对Fibers不熟悉,为什么会这样呢?

2 回答

  • 28

    这里的问题是你传递给 Stripe.charges.create 的回调函数是异步调用的(当然),所以's happening outside the current Meteor' s Fiber .

    修复它的一种方法是创建自己的 Fiber ,但最简单的方法是用 Meteor.bindEnvironment 包装回调,所以基本上

    Stripe.charges.create({
      // ...
    }, Meteor.bindEnvironment(function (error, result) {
      // ...
    }));
    

    编辑

    正如在另一个答案中所建议的那样,另一个可能更好的模式是使用 Meteor.wrapAsync 辅助方法(参见docs),它基本上允许您将任何异步方法转换为具有光纤感知功能且可以同步使用的功能 .

    在您的具体情况下,一个等效的解决方案是写:

    let result;
    try {
      result = Meteor.wrapAsync(Stripe.charges.create, Stripe.charges)({ /* ... */ });
    } catch(error) {
      // ...
    }
    

    请注意第二个参数传递给 Meteor.wrapAsync . 它是为了确保原始 Stripe.charges.create 将收到正确的 this 上下文,以防万一需要它 .

  • 5

    您可能想查看http://docs.meteor.com/#/full/meteor_wrapasync的文档 .

相关问题