如何消费braintree返回的节点流
How to consume the node stream returned by 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: []
}
来自 nodejs 流文档
http://nodejs.org/api/stream.html#apicontent
A stream is an abstract interface implemented by various objects in Node. For >example a request to an HTTP server is a stream, as is stdout. Streams are >readable, writable, or both. All streams are instances of 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);
});
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: []
}
来自 nodejs 流文档
http://nodejs.org/api/stream.html#apicontent
A stream is an abstract interface implemented by various objects in Node. For >example a request to an HTTP server is a stream, as is stdout. Streams are >readable, writable, or both. All streams are instances of 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);
});