【问题标题】:Using Meteor.wrapAsync to wrap a callback inside a method使用 Meteor.wrapAsync 在方法中包装回调
【发布时间】:2023-03-16 05:12:01
【问题描述】:

这个 Meteor 代码给出了错误:

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

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

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

      );
    }
});

【问题讨论】:

    标签: meteor


    【解决方案1】:

    包裹在 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();
        }
    });
    

    或者尝试用 Fiber 包裹它

    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
        },
    });
    

    【讨论】:

    • 在使用 wrapAsync 的第一个代码块中,{arg_object} 是否也包含gateway.transaction.sale 的回调?还有如何处理future,因为它是未解析的变量?
    • 我也尝试了第二个代码块,但仍然得到同样的错误。
    • arg_object 不包含回调,因为您正在异步包装它。您以与带有回调的常规方法相同的方式处理方法返回。 Meteor.call('createTransaction',nonce,payment,function(err,res){...})
    • 我将用我如何处理条纹支付来更新答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 2014-05-10
    • 1970-01-01
    相关资源
    最近更新 更多