无法使用 streams/highland.js 从结果中获取 mongodb 的数据

unable to fetch data from mongodb from results using streams/highland.js

我是流的新手,我正在尝试使用 reactive-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/

我能够使用这种方法检索有效载荷,不确定这是否是最好的方法,非常感谢任何其他建议或解释。

exports.findAll = function (err, res) {
    query.find()
        .map(JSON.stringify)
        .toArray(function(x){
          res.end(x + '')
        })
}

我不太确定 reactive-superglue 在这里为您做了什么。看起来只是一个让不同数据源响应的highland shortcut的汇编

您可以使用 highland 直接这样做:

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() 没有 return 结果。您应该使用 Stream.each 来迭代每个元素,或者使用 Stream.toArray 将它们全部作为一个数组。

顺便说一句,我是 reactive-superglue 的作者。 reactive-superglue 是我(正在进行的工作)在 highland.js

之上构建的 highland 流的实际使用

干杯!