【问题标题】:Braintree Transaction.search in Meteor Server流星服务器中的 Braintree Transaction.search
【发布时间】:2017-05-09 15:37:29
【问题描述】:

如何等待 Braintree Transaction.search() 函数返回所有数据。 现在它不等待,只是返回未定义的返回值。 这是代码 我尝试使用 Meteor.asynwrap 但这也不起作用。 `

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

【问题讨论】:

  • 您使用的是哪个版本的 Meteor?

标签: javascript asynchronous meteor wait braintree


【解决方案1】:

找到了一种使它起作用的方法: 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

现在我可以得到交易。希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 2014-12-01
    • 2013-10-04
    • 2014-01-24
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多