5

我一直在使用 Meteor 和 stripe 包来尝试吸引客户。所以首先我有我的客户端代码,它调用服务器上的一个方法,所以当点击我在 client.js 中:

Meteor.call('usersignup', function (error, result) {
    console.log (result);
});

所以这会调用 server.js 上的方法:

var Future = Npm.require('fibers/future');
var stripe = StripeAPI('my key'); // secret stripe API key

Meteor.methods({

    usersignup: function(cusEmail){
        var fut = new Future();

        stripe.customers.create(
            { email: cusEmail },
            function(err, customer) {
                if (err) {
                    console.log(err);
                    fut.ret;
                }
                fut.ret(customer);
            }
            );
        return fut.wait();
    },

    userfail: function(cusid){
        var fut = new Future();

        stripe.customers.retrieve(cusid, function(err, result) {
            if(err){
                    console.log(err);
                    fut.ret;
                }
                fut.ret(err, result);
            });
        return fut.wait();
    }

});

现在,当我登录到 stripe.com 仪表板时,这可以工作并创建一个客户,但我正在尝试将响应至少返回给客户,至少现在是客户 ID,并在控制台中打印它。这是我似乎无法让它工作的地方。当我执行 console.log(result) 时,它会记录未定义。有任何想法吗?

编辑:所以我现在将光纤和条带键作为全局变量并且没有收到错误,但返回似乎没有返回任何值。所以在客户端我有:

'click #signupsubmit': function (event) {
    console.log("hello");
    var whatis = getVal(); // function gets value of forms and returns object
    var testid;
    var cusid = Meteor.call('usersignup', whatis.email, function (error, result) {
        if (error) {
            console.log(err.message);
            return;
        }
        console.log(result);
        console.log("meteor call");
        testid = result;
        return (result);
    });
    console.log("outside call");
    console.log(testid);
    console.log(cusid);
    },
});

所以我一直在运行一些console.log 测试,它似乎执行了meteor.call 并继续下线。testid 和 cusid 的 Console.log 都返回未定义,但几秒钟后,我收到了 result 的 console.log 和来自 meteor.call 内部的字符串“meteor call”。有没有办法等待流星调用完成然后运行我的点击功能中的其余部分?所以控制台输出将如下所示:

  • “你好”
  • “外线电话”
  • 测试ID未定义
  • cusid 未定义
  • “流星呼唤”
  • “结果”
4

2 回答 2

9

请记住,条带 API 不使用 Fibers。您需要手动将其放入。回调没有到达客户端,因为到那时它已经得到了响应(它的异步)

在将结果返回给客户端之前,您可以使用类似这样的方法来等待条带回调的结果:

var stripe = StripeAPI('mykeygoeshere');  // secret stripe API key
var Future = Npm.require('fibers/future');

var fut = new Future();

stripe.customers.create(
    { email: 'hello@example.org' },
    function(err, customer) {
        if (err) {
            console.log(err.message);
            fut.ret;
        }
        fut.ret("customer id", customer.id);
    }
);
return fut.wait();

这里Future使用了 a ,它等待从条带回调接收到结果,然后再将结果返回给客户端。

可以在 Fibers/Futures & Synchronous Callbacks 上找到更多信息,包括如何使用它们以及何时使用它们:

  1. Meteor:在 Meteor.method 中调用异步函数并返回结果
  2. https://github.com/laverdet/node-fibers
  3. https://gist.github.com/possibilities/3443021
于 2013-04-20T20:02:29.993 回答
1

这里有一些更简单的东西。Meteor 现在有Meteor.wrapAsync()用于这种情况:

var stripe = StripeAPI("key");    
Meteor.methods({

    yourMethod: function(callArg) {

        var charge = Meteor.wrapAsync(stripe.charges.create, stripe.charges);
        charge({
            amount: amount,
            currency: "usd",
            //I passed the stripe token in callArg
            card: callArg.stripeToken,
        }, function(err, charge) {
            if (err && err.type === 'StripeCardError') {
              // The card has been declined
              throw new Meteor.Error("stripe-charge-error", err.message);
            }

            //Insert your 'on success' code here

        });
    }
});

我发现这篇文章真的很有帮助: Meteor: Proper use of Meteor.wrapAsync on server

于 2015-01-07T06:48:16.450 回答