2014-10-12 43 views
2

我一直在努力工作Meteor.WrapAsync我已閱讀Meteor wrapAsync syntax回答,這個視頻https://www.eventedmind.com/feed/meteor-meteor-wrapasync和我只是無法計算如何return來自Stripe調用的響應。我用console.log來打印這些步驟,並且我已經到達了第4個擲數,這意味着,我到達stripe服務器並獲得響應,但之後我看不到爲什麼console.log(5)它沒有打印。請如果有人能幫我理解爲什麼它的wrapAsyn它不返回條紋回調?Meteor.WrapAsync不返回值

//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: "[email protected]" 
     }, 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"); 
    } 

//電流輸出1,2,3,4,但從來沒有5 :(

編輯

這是我結束具有條紋功能感謝支持

var syncFunction = Meteor.wrapAsync(_stripe.charges.create, _stripe.charges); 
    var response = syncFunction({ 
     amount: 1000, // amount in cents, again 
     currency: "cad", 
     card: stripeToken.id, 
     description: "[email protected]" 
    }); 
+0

控制檯中的任何錯誤?我猜想會發生這種情況,因爲你沒有將函數綁定到對象上:'Meteor.wrapAsync(stripe.charge.process,stripe.charge)' – imslavko 2014-10-12 21:01:42

+0

沒有任何錯誤,並且在一開始就做到了這一點讓我試試吧現在再次... – ncubica 2014-10-12 21:22:59

回答

4

你在這裏包裝了錯誤的功能,Meteor.wrapAsync在一個同步函數中轉換了一個異步函數(這意味着一個函數將它的結果通過回調函數傳遞給調用者)

您傳遞給Meteor.wrapAsync的函數沒有回調作爲最終參數,而應該換行_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: "[email protected]" 
    }); 
    return response; 
}; 

如果你想處理錯誤,你應該在調用stripe.charge時使用try/catch塊。

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

我看你使用alert記錄你的錯誤,你想在客戶端上使用Meteor.wrapAsyncMeteor.wrapAsync意味着在服務器上使用,因爲提供同步查看執行所需的環境在Node.js中可用,而不是在瀏覽器中。

+0

男人我不是kiddidng剛纔我發現我自己這個愚蠢的錯誤,我是寫這個答案,但你做了之前謝謝你,你應該得到答案。這對未來會有所幫助。 – ncubica 2014-10-12 21:46:11

+0

是的即時通訊在服務器端使用它,現在一切工作完全謝謝... – ncubica 2014-10-12 21:48:28

+0

但我不能得到完整的原始錯誤對象,但。我的解決方案粘貼在這裏:https://github.com/meteor/meteor/issues/2774 – 2015-01-21 04:31:53