使用 Meteor.wrapAsync 将回调包装在方法中



这个流星代码给出了错误:

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

我尝试Meteor.bindEnvironment无济于事,想尝试Meteor.wrapAsync。我无法从文档中弄清楚。有人可以帮我语法吗?感谢

Meteor.methods({
'createTransaction':
    function (nonceFromTheClient, Payment) {
      let user = Meteor.user();
      gateway.transaction.sale(
        {
          arg_object
        },
          function (err, success) {
            if (!err) {
              //do stuff here
            }
          }
      );
    }
});

Wrap in Meteor.wrapAsync

Meteor.methods({
'createTransaction':
    function (nonceFromTheClient, Payment) {
      this.unblock();
      let user = Meteor.user();
      var sale = Meteor.wrapAsync(gateway.transaction.sale);
      var res = sale({arg_object});
      future.return(res);
      return future.wait();
    }
});

或者尝试用纤维包裹

var Fiber = Npm.require('fibers');
Meteor.methods({
'createTransaction': function (nonceFromTheClient, Payment) {
    Fiber(function() {
      let user = Meteor.user();
      gateway.transaction.sale(
        {
          arg_object
        },
          function (err, success) {
            if (!err) {
              //do stuff here
            }
          }
      );
   }).run()
  }
});

更新:这是我使用 Async.runSync 和 Meteor.bindEnvironment 处理条纹的方式

var stripe = require("stripe")(Meteor.settings.private.StripeKeys.secretKey);
Meteor.methods({
    'stripeToken': function() {
        this.unblock();
        var future = new Future();
        var encrypted = CryptoJS.AES.encrypt(Meteor.userId(), userIdEncryptionToken);
        future.return(encrypted.toString());
        return future.wait();
    },
    'stripePayment': function(token) {
        var userId = Meteor.userId();
        var totalPrice = 0;
        //calculate total price from collection
        totalPrice = Math.ceil(totalPrice * 100) / 100;
        userEmail = Meteor.users.findOne({
            '_id': userId
        }).emails[0].address;
        // Create a charge: this will charge the user's card
        var now = new Date();
        Async.runSync(function(done) {
            var charge = stripe.charges.create({
                amount: Math.ceil(totalPrice * 100), // Amount in cents // coverting dollars to cents
                currency: "usd",
                source: token,
                receipt_email: userEmail,
                description: "Charging"
            }, Meteor.bindEnvironment(function(err, charge) {
                if (err) {
                    //handle errors with a switch case for different errors     
                    done();
                } else {
                    //handle res, update order
                }
            }));
        }); // Async.runSync
    },
});

最新更新