2017-05-09 2 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 

이제 거래를 할 수 있습니다. 도움이 되었으면 좋겠어요