2

I been trying to make work Meteor.WrapAsync I have read Meteor wrapAsync syntax answer, this video https://www.eventedmind.com/feed/meteor-meteor-wrapasync and I just cant figure how to return the response from the call from Stripe. I'm using console.log to print the steps, and I been reaching throw number 4 which mean, Im reaching stripe server and getting the response, but after that I can't see why console.log(5) its not printing. please if somebody can help me to understand why its the wrapAsyn its not returning the stripe callback?

    //this functions are part of an anonymous function and running in the server side of meteor
    stripe.charge = function (stripeToken) {
        // get a sync version of our API async func
        var strypeChargeSync = Meteor.wrapAsync(stripe.charge.process);

        // call the sync version of our API func with the parameters from the method call

        console.log("1");

        var response = strypeChargeSync(stripeToken);

        console.log("5" + response); ///// this never get print / log
        return response;
    }

    stripe.charge.process = function(stripeToken){
        var _stripe = StripeAPI(stripeKey);
        console.log("2");
        var charge = _stripe.charges.create({
            amount: 1000, // amount in cents, again
            currency: "cad",
            card: stripeToken.id,
            description: "paid@whatever"
        }, function(err, charge) {
            if (err && err.type === 'StripeCardError') {
                alert("Sorry we couldn't charge the money: " + err);
                //console.log(err);
            }else{
                console.log("4");
                //console.log(charge);
                return charge;
            }
        });
        console.log("3");
    }

//current output 1,2,3,4 but never 5 :(

EDIT

this is how I end having the Stripe function thanks for the support

    var syncFunction = Meteor.wrapAsync(_stripe.charges.create, _stripe.charges);
    var response = syncFunction({
        amount: 1000, // amount in cents, again
        currency: "cad",
        card: stripeToken.id,
        description: "paid@whatever"
    });
Community
  • 1
  • 1
ncubica
  • 8,169
  • 9
  • 54
  • 72
  • Any errors in the console? I would guess it happens because you didn't bind the function to the object: `Meteor.wrapAsync(stripe.charge.process, stripe.charge)` – imslavko Oct 12 '14 at 21:01
  • none errors,and also did that at the very beginning let me try it right now again... – ncubica Oct 12 '14 at 21:22

1 Answers1

4

You are wrapping the wrong function here, Meteor.wrapAsync transforms an async function (this means a function which transmits its result to the caller via a callback) in a synchronous one.

The function you pass to Meteor.wrapAsync does not have a callback as final argument, you should instead wrap _stripe.charge.create.

stripe.charge = function (stripeToken) {
  var _stripe = StripeAPI(stripeToken);
  var stripeChargeSync = Meteor.wrapAsync(_stripe.charge.create,_.stripe.charge);
  var response = stripeChargeSync({
    amount: 1000, // amount in cents, again
    currency: "cad",
    card: stripeToken.id,
    description: "paid@whatever"
  });
  return response;
};

If you want to handle errors, you should use a try/catch block when calling stripe.charge.

try{
  stripe.charge(STRIPE_TOKEN);
}
catch(exception){
  console.log("Sorry we couldn't charge the money",exception);
}

I see you are logging your error using alert, are you trying to use Meteor.wrapAsync on the client ? Meteor.wrapAsync is meant to be used on the server because the environment needed to provide synchronous-looking execution is available in Node.js, not the browser.

saimeunt
  • 22,666
  • 2
  • 56
  • 61
  • man Im not kiddidng just right now I discover by my self this dumb mistake I was to write this answer but you did it before thanks man you deserve the answer. this is gonna help a lot in the future to some body. – ncubica Oct 12 '14 at 21:46
  • yes im using it on the server side, now everything is working perfectly thanks... – ncubica Oct 12 '14 at 21:48
  • u cant get the full original error object though. My solution is pasted here: https://github.com/meteor/meteor/issues/2774 – faceyspacey.com Jan 21 '15 at 04:31
  • This helped me greatly ! Thank you guys for this ! – jremi May 19 '16 at 06:00