带有patrickml的meteor:braintree代码必须始终在光纤内运行

时间:2017-03-23 23:39:05

标签: meteor braintree

此Meteor服务器代码(主要是从其他人复制和粘贴)会出现此错误:

  

错误:Meteor代码必须始终在光纤内运行。尝试使用Meteor.bindEnvironment包装传递给非Meteor库的回调。

它可以很好地将付款发送到Braintree沙箱,但是一旦完成,我需要在服务器上执行操作"查看服务器代码的底部",如何修复它?

我试过'createTransaction: Meteor.wrapAsync(function (nonceFromTheClient, Payment) {...});),但无济于事。

//client.account.js
Template.account.onRendered(function () { //6a
  Meteor.call('getClientToken', function (error, clientToken) {
    if (error) {
      console.log(error);
    } else {
      braintree.setup(clientToken, "dropin", {
        container: "payment-form",
        onPaymentMethodReceived: function (response) {
          var nonce = response.nonce;
          Meteor.call('btCreateCustomer', function (error) {
            if (error) {
              throw new Meteor.Error('customer-creation-failed');
            } else {
              let e = document.getElementById("invoice");
              let label = e.options[e.selectedIndex].text;
              let Payment = {
                amount: /\$(.*?) /g.exec(label)[1],
                period: /(.*? .*?) /g.exec(label)[1]
              };
              Meteor.call('createTransaction', nonce, Payment, function (error) {
                if (!error) {
                  //do stuff.
                }
              });
            }
          });
        }
      });
    }
  });
});

//server.main.js
Meteor.methods({
  'getClientToken': function (clientId) {
    let generateToken = Meteor.wrapAsync(gateway.clientToken.generate, gateway.clientToken);
    let options = {};
    if (clientId) {
      options.clientId = clientId;
    }

    let response = generateToken(options);
    return response.clientToken;
  },
  'btCreateCustomer': function () {
    let user = Meteor.user();
    let customerData = {
      email: user.emails[0].address
    };

    gateway.customer.create(customerData, function (error, response) {
      if (!error) {
        Meteor.users.update(user._id, {
          $set: {
            customerId: response.customer.id
          }
        });
      }
    });
  },
  'createTransaction': function (nonceFromTheClient, Payment) {
    let user = Meteor.user();
    gateway.transaction.sale({
      amount: Payment.amount,
      paymentMethodNonce: nonceFromTheClient, // Generated nonce passed from client
      customer: {
        id: user.customerId
      },
      options: {
        submitForSettlement: true, // Payment is submitted for settlement immediately
        storeInVaultOnSuccess: true // Store customer in Braintree's Vault
      }
    }, function (err) {
      if (!err) {
        // ------------- next line fail due to Fiber issue ------------------ 
        console.log('Paid ' + Payment.amount + " for period " + Payment.period);
      }
    });
  }
});
<template name="account">
  <div id="account">
    <form role="form">
      <div class="row">
        <div class="col-md-6 col-xs-12">
          <div id="payment-form"></div>
          <button type="submit" class="btn btn-success">Submit</button>
        </div>
      </div>
    </form>
  </div>
</template>

1 个答案:

答案 0 :(得分:0)

尝试在Meteor.bindEnvironment中包装回调:

gateway.transaction.sale({/* args */}, Meteor.bindEnvironment(function(err) {
  if (!err) {
    console.log('Nice!');
  }
}));