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