You should use find method of mongodb to get the documents from a collection. And then use cursor to iterate through the documents.
var getRoomlist = function(db, callback) {
var cursor =db.collection('roomlist').find( );
cursor.each(function(err, doc) {
assert.equal(err, null);
if (doc != null) {
return callback(false, doc);
} else {
return callback(true, null);
}
});
};
Then you can use your route /roomlist, to call the function getRoomlist when it gets hit and render the doc.
router.get('/roomlist', function(req, res){
programLogic(function(err, doc){
if(err){
console.log(err);
}
return res.send(doc);
});
});
If you still don't get it, just clone this and run through it
var programLogic = function (cb){
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
getRoomlist(db, function(err, doc) {
if(err){
console.log(err);
}
return cb(false, doc);
});
});
}// programLogic ends
var getRoomlist = function(db, callback) {
var cursor =db.collection('roomlist').find( );
cursor.each(function(err, doc) {
assert.equal(err, null);
if (doc != null) {
return callback(false, doc);
} else {
return callback(true, null);
}
});
};
router.get('/roomlist', function(req, res){
programLogic(function(err, doc){
if(err){
console.log(err);
}
return res.send(doc);
});
});