2013-04-20 55 views
5

我一直在與流星和條紋包合作,試圖讓客戶。所以首先我有一個調用服務器上的方法,我的客戶端代碼,所以點擊時我在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都返回undefined,但幾秒鐘後,我收到了console.log的結果以及meteor.call中的字符串「meteor call」。有沒有辦法等待流星調用完成,然後運行我的點擊功能中的其餘部分?所以控制檯輸出就會像:

  • 「你好」
  • 「外線電話」
  • 測試ID未定義
  • cusid不確定
  • 「流星呼叫」
  • 「結果」

回答

9

請記住,條紋API不使用光纖。你需要手動把它。回調沒有到達客戶端,因爲到那時它已經得到了響應(異步)

在結果返回給客戶端之前,可以使用類似的方式來等待條帶回調的結果:

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

var fut = new Future(); 

stripe.customers.create(
    { email: '[email protected]' }, 
    function(err, customer) { 
     if (err) { 
      console.log(err.message); 
      fut.ret; 
     } 
     fut.ret("customer id", customer.id); 
    } 
); 
return fut.wait(); 

這裏一個Future被使用並且它等待從條紋回調結果返回到客戶端之前被接收的結果。

更多信息可以在纖維/期貨中找到&同步回調incuding如何去他們&何時使用它們:

  1. Meteor: Calling an asynchronous function inside a Meteor.method and returning the result
  2. https://github.com/laverdet/node-fibers
  3. https://gist.github.com/possibilities/3443021
+0

這個工作很大,但我去的時候做另一種方法,例如將刪除客戶它拋出一個錯誤('未來解決不止一次')。所以現在有人點擊提交獲取表單中的所有信息並創建一個stripe.customer,然後創建一個Meteor.user,但是說Accounts.createUser拋出一個錯誤我運行一個方法來刪除條紋客戶。在我調用我的stripe del方法後,它給出了錯誤。 – asiammyself 2013-04-22 19:10:49

+0

你能發佈更新後的代碼嗎?我需要多一點繼續,但我認爲可能提供多個回報,因此您必須確保fut.ret只運行一次 – Akshat 2013-04-23 08:30:36

+0

更新的代碼。使未來成爲全球變種。不知道這是否是正確的方式來處理它,但現在工作。功能似乎還沒有等待返回見編輯後。 – asiammyself 2013-04-24 18:50:22

1

這裏的更簡單一些。流星現在有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

+0

如何找出客戶發生了什麼事?調用此服務器方法後,我只得到未定義的結果。 – quape 2016-01-24 13:44:28