1
我剛開始學習節點流,我正在使用MongoClient(MongoClient Cursor Doc)。在本文檔中,它指出我可以將返回的查詢作爲文檔流獲取。像這樣:MongoClient節點遊標流和數據管道
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
var col = db.collection('streams');
// Insert a single document
col.insert([{a:1}, {a:1}, {a:1}], function(err, r) {
assert.equal(null, err);
assert.equal(3, r.result.n);
// Get the results using a find stream
var cursor = col.find({});
cursor.on('data', function(doc) {
console.dir(doc);
});
cursor.once('end', function() {
db.close();
});
});
});
現在我試圖使用由var cursor = col.find({});
創建的管道流進through2
,拿出數據聽衆,並最終像這樣:
var cursor = col.find({});
cursor.pipe(through2(function (buf, _, next) {
console.log('chunkString: ', buf.toString());
next();
}));
不過,我得到這個錯誤:
/Users/blah/projects/mybuz/blah-ad/node_modules/mongodb/lib/utils.js:97
process.nextTick(function() { throw err; });
^
TypeError: Invalid non-string/buffer chunk
at validChunk (/Users/blah/projects/mybuz/blah-ad/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:164:14)
不知道我做錯了,因爲我從可讀流傳輸到雙工流,只是在控制檯上輸出該值。
查找這個答案? –
負面的,我放棄了,只是使用了常規的回調,這在回調vs流方面很糟糕。 – britztopher