简体   繁体   中英

Meteor.call not returning a response

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

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

The server method is defined as:

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

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.

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?

Thanks for the help!

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM