2015-03-02 99 views
1

Braintree提供了搜索事务的api。 Braintree提供了一个例子,但我不知道如何阅读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: [] 
} 

回答

2

从流的NodeJS文档

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

流是通过在节点的各种对象实现的抽象接口。例如,对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); 
}); 
+0

好吧。我曾尝试解析对象,但无法这样做。 someWritableStream.on(“end”,function(){ // Do接收到所有块后的某些事情 console.log(completeData); JSON.parse(completeData); }); – Prem 2015-03-02 16:29:33