I am trying to call a function that retrives all the document from a collection and returns the data. Now, everything works fine but for some reason , I am getting an undefined response from my function callback. What could I be doing wrong?
var findImages = function(db, callback) {
var s ="";
db.collection('imageinfo', function(err, collection) {
collection.find().toArray(function(err, items) {
for(var i=0;i<items.length;i++){
console.log("imagename",items[i].imagename);
s =s + items[i].imagename+",";
}
if(i==items.length){
callback(s);
}
});
});
};
app.get("/fetchrecords",function(req,res){
console.log("entered");
findImages(db,function(req,res){
console.log("res",res);
});
res.end("Sent records");
});
... function(err, items) { if (err) return callback(err) ....