Meteor.call 不返回响应 [英] Meteor.call not returning a response

查看:43
本文介绍了Meteor.call 不返回响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 Stripe 使用新的 Promise 支持.

I'm trying to get Stripe working with the new promises support.

使用 checkout,我获取令牌并将其发送到服务器:

Using checkout, I get the token and send it to the server:

Meteor.call('submit_charge', res.id, fee, name, reg, function (err, res) {
    console.log(err, res);
});

服务器方法定义为:

submit_charge: function(tok, amt, name, reg) {
    var Stripe = StripeAPI('privatekey');
    console.log('Submitting charge for ' + name);
    Stripe.charges.create({
        amount: amt,
        currency: "usd",
        card: tok,
        description: "Payment - " + name,
        metadata: {
            'reg': reg
        },
    }).then(function(charge) {
        console.log('Charge: ' + charge.id);
        return charge.id;
    }, function(err) {
        console.log('Error: ' + err);
        return 0;
    });
}

我可以调用该方法并执行,但不返回任何内容.Meteor.call 中的 console.log(err, res) 对两者都返回 undefined.

I can call the method and it executes, but doesn't return anything. The console.log(err, res) in the Meteor.call returns undefined for both.

充电过程...并且 console.logs 显示来自 Stripe 的充电 ID,因此它似乎不是异步问题.

The charge processes... and the console.logs show the charge ID from Stripe, so it doesn't seem to be an async issue.

我在这里错过了一些非常基本的东西吗?

Am I missing something incredibly basic here?

感谢您的帮助!

推荐答案

你必须使用同步 javascript:

You have to use synchronous javascript:

submit_charge: function(tok, amt, name, reg) {
    var Stripe = StripeAPI('privatekey');
    console.log('Submitting charge for ' + name);

    var createCharge = Meteor._wrapAsync(Stripe.charges.create.bind(Stripe.charges));

    try {
        var result = createCharge({
            amount: amt,
            currency: "usd",
            card: tok,
            description: "Payment - " + name,
            metadata: {
            'reg': reg
        });

        return result;
    }
    catch(e) {
        //Error
        console.log(e);
    }
}

基本上,您正在尝试从回调中返回数据.您需要return 数据到您的meteor 方法,而不是then 中的函数.

Basically you're trying to return data from within a callback. You need to return data to your meteor method and not the function in the then.

使用 Meteor._wrapAsync 使用 Fibers 等待事务完成,然后返回值或抛出错误(因此是 try/catch)以获取错误.

Using Meteor._wrapAsync uses Fibers to wait until the transaction is complete then returns the value or throws an error (hence the try/catch) to get the error.

这篇关于Meteor.call 不返回响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆