无法从结果中使用streams / highland.js从mongodb中获取数据
我是新来的stream,我试图从我的集合使用反应superglue / highland.js( https://github.com/santillaner/reactive-superglue )获取数据。
var sg = require("reactive-superglue") var query = sg.mongodb("mongodb://localhost:27017/qatrackerdb").collection("test1") exports.findAll = function (err, res) { query.find() .map(JSON.stringify) .done(function(data) { console.log(data) res.end(data) }) }
我的卷毛请求:
curl -i -X GET http://localhost:3000/queries/
我不是很确定这里有什么reactive-superglue
在为你做。 看起来这只是一个高地快捷方式的汇编,用于获取不同的数据源来响应。
你可以使用高地直接这样做:
var collection = sg.mongodb("mongodb://localhost:27017/qatrackerdb").collection("test1"); return h( collection.find({}) ) .map(h.extend({foo: "bar"}) .pipe(res);
编辑:上面的代码片段仍然使用reactive-superglue
,但是您可以使用节点mongo驱动程序:
var url = 'mongodb://localhost:27017/qatrackerdb'; MongoClient.connect(url, function(err, db) { h( db.collection("test1").find({}) ) .map(h.extend({foo: "bar"}) .pipe(res); });
你的代码段不起作用,因为highland.js的.done()不返回结果。 您应该使用Stream.each来迭代每个元素或Stream.toArray以将它们全部作为数组来获取。
顺便说一句,我是反应超强的作者。 被动超级胶是我的(工作进行中)承担高地的stream的实际使用,build立在highland.js之上
干杯!
我能够使用这种方法检索有效载荷,不知道这是否是最好的方法,将非常感谢任何其他build议或解释。
exports.findAll = function (err, res) { query.find() .map(JSON.stringify) .toArray(function(x){ res.end(x + '') }) }