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

289 Views Asked by At

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).

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/
3

There are 3 best solutions below

0
On BEST ANSWER

I'm not really sure what reactive-superglue is doing for you here. 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:

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:

var url = 'mongodb://localhost:27017/qatrackerdb';
MongoClient.connect(url, function(err, db) {
  h( db.collection("test1").find({}) )
    .map(h.extend({foo: "bar"})
    .pipe(res);
});
0
On

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 + '')
        })
}
0
On

Your code snippet does not work because highland.js's .done() does not return the result. You should either use Stream.each to iterate each element or Stream.toArray to get them all as an array.

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

Cheers!