如何使用braintree返回的节点流

时间:2015-03-02 11:00:47

标签: node.js braintree

Braintree提供了一个api来搜索交易。 Braintree提供了一个例子,但我不知道如何读取braintree返回的节点流。请查看以下代码段:

var stream = gateway.transaction.search(function (search) {
  search.paymentMethodToken().is("h337xg");
});

stream.pipe(someWritableStream);

//When I try to print the stream in console, I get the following result:

{ 
   _readableState:
   { highWaterMark: 16384,
     buffer: [],
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: false,
     ended: false,
     endEmitted: false,
     reading: false,
     calledRead: false,
     sync: true,
     needReadable: false,
     emittedReadable: false,
     readableListening: false,
     objectMode: true,
     defaultEncoding: 'utf8',
     ranOut: false,
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null },
  readable: true,
  domain: null,
  _events: {},
  _maxListeners: 10,
  searchResponse: { stream: [Circular], success: true },
  currentItem: 0,
  currentOffset: 0,
  bufferedResults: [] 
}

1 个答案:

答案 0 :(得分:2)

来自nodejs流文档

http://nodejs.org/api/stream.html#apicontent

  

流是由Node中的各种对象实现的抽象接口。对于>示例,对HTTP服务器的请求是流,stdout也是如此。流是可读的,可写的或两者兼有。所有流都是EventEmitter的实例

您应该使用流的数据事件来捕获流所接收的数据。当从蒸汽接收到完整数据时,将调用流的结束事件

completeData = ""
someWritableStream.on("data", function(chunk){
    //Do Something With the chunk of data. You might want to concat the stream
    completeData += chunk;
});

someWritableStream.on("end", function(){
    //Do Something after the all the chunks are received.
    console.log(completeData);
});