2017-05-09 7 views
1

Braintree Transaction.search()関数がすべてのデータを返すのを待つ方法はありますか。 今は待たずに戻り値が返されます。 コードは ですMeteor.asynwrapを使用しようとしましたが、これも機能しません。 `Meteor ServerのBraintree Transaction.search

function getTrxns(cid) { 
    var future = new Future(); 
    var trxns = []; 
    var i = 0 
    var stream = gateway.transaction.search(function (search) { 
         r =  search.customerId().is(cid)}); 

    stream.on("data", function(data){ 
     i = i+1 
     trxns.push({ 
     'id':data.id, 
     'amount':data.amount, 
     'crtDt': data.createdAt, 
     'ccType': data.creditCard.cardType, 
     'currency': data.currencyIsoCode, 
     'last4': data.creditCard.last4, 
     'expdt': data.creditCard.expirationDate 
     }); 
}); 
stream.on("end", function(){ 
    // print the output in console 
    console.log('End Stream cnt: '+i); 
    return trxns; 
}); 

stream.resume(); 
} 

Meteor.methods({ 
findCustTrxns: function() { 
     var btId = Meteor.user().custBtId;  
     if (!btId) { return []; }; 
     console.log('findCustTrxns cusBtId: '+btId); 
     var xx = getTrxns(btId); 
       console.log('xx len :'+xx.length); 
} 

}); 


OUTPUT is: 

I20170509-15:22:09.095(0)?  findCustTrxns cusBtId: 232057823 
I20170509-15:22:09.095(0)?  Exception while invoking method 'findCustTrxns' TypeError: Cannot read property 'length' of undefined 
I20170509-15:22:09.095(0)?  End Stream cnt: 56 

+0

Meteorのどのバージョンを使用していますか? – DSK

答えて

0

はそれを動作させるための方法を見つけた: 1.追加コールバック関数
関数getTrxns(CID、コールバック
2.コールバックを呼び出しstream.on( 'end; ..)コードは次のとおりです

  function getTrxns(cid,callback) { 

     var trxns = []; 
     var i = 0 
     var stream = gateway.transaction.search(function (search) { 
          r =  search.customerId().is(cid)}); 

     stream.on("data", function(data){ 
      i = i+1 
      trxns.push({ 
       'id':data.id, 
      }); 
    }); 
    stream.on("end", function(){ 
     // print the output in console 
     console.log('End Stream cnt: '+i); 
     callback('', trxns); 
    }); 

    stream.resume(); 
    } 



3. Changed the Meteor Method : 

findCustTrxns: function(btId) { 


     if (!btId) { return []; }; 
     console.log('findCustTrxns cusBtId: '+btId); 

var trxns = []; 
    var i = 0; 

     var fn = Meteor.wrapAsync(getTrxns); //made it a synchronous call 
     try { 
      var res = fn(btId); 
      if (res) { 
       console.log('Got data from getTrxns '); 
       return res; 
      } 
     } catch(err) { 
       console.log('Error calling hetTrxns '+err); 
     } 
}, //findCustTrxns 

今、私はトランザクションを得ることができます。希望しますか?