David Trinh David Trinh - 5 months ago 25
Node.js Question

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

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/

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);
});