简体   繁体   English

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

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

I am new to streams and I am trying to fetch the data from my collection using reactive-superglue/highland.js ( https://github.com/santillaner/reactive-superglue ).我是流的新手,我正在尝试使用 react-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)
        })
}

my curl request:我的卷曲请求:

curl -i -X GET http://localhost:3000/queries/

Your code snippet does not work because highland.js's .done() does not return the result.您的代码片段不起作用,因为 highland.js 的 .done() 不返回结果。 You should either use Stream.each to iterate each element or Stream.toArray to get them all as an array.您应该使用 Stream.each 迭代每个元素或使用 Stream.toArray 将它们全部作为数组获取。

BTW, I'm reactive-superglue's author.顺便说一句,我是反应强力胶的作者。 reactive-superglue is my (work-in-progress) take on real-world usage of highland's streams, built on top of highland.js react-superglue 是我(正在进行中)在 highland 流的现实世界中的使用,建立在 highland.js 之上

Cheers!干杯!

I was able to retrieve the payload using this approach, not sure if this is the best way, would greatly appreciate any other suggestions or explanations.我能够使用这种方法检索有效负载,不确定这是否是最好的方法,非常感谢任何其他建议或解释。

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

I'm not really sure what reactive-superglue is doing for you here.我不太确定reactive-superglue在这里为你做什么。 It looks like it's just a compilation of highland shortcuts for getting different data sources to respond.看起来只是高地捷径的汇编,用于获取不同数据源的响应。

You can use highland to do this directly like this:您可以使用 highland 直接执行此操作:

var collection = sg.mongodb("mongodb://localhost:27017/qatrackerdb").collection("test1");
return h( collection.find({}) )
    .map(h.extend({foo: "bar"})
    .pipe(res);

Edit: The above snippet still uses reactive-superglue , but you could just use the node mongo driver:编辑:上面的代码片段仍然使用reactive-superglue ,但你可以只使用 node 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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM