【发布时间】:2015-08-16 15:18:46
【问题描述】:
我刚刚开始学习节点流,我正在使用 MongoClient (MongoClient Cursor Doc)。在本文档中,它声明我可以将返回的查询作为文档流获取。像这样:
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)
不知道我做错了什么,因为我正在从可读流传输到双工流,并且只是在控制台上输出该值。
【问题讨论】:
-
找到答案?
-
否定,我放弃了,只使用常规回调,这在回调与流方面很糟糕。