var newid = [];
var newid = ["55dd605d636145b80f52b85e","55dd652b717d72200538cb72","55dd8f7bbc96c06808a245ae"];
for(var i = 0;i<=2;i++)
{
var fileId = mongoose.Types.ObjectId(newid[i]);
var gridStore = new mongodb.GridStore(db, fileId, 'r');
gridStore.open(function (err, gridStore) {
if (err) {
res.writeHead(500);
return res.end();
}
//return
res.writeHead(200, {'Content-Type': 'image/png'});
gridStore.stream(true).on('end', function() {
}).pipe(res);
});
}
I am using the above code to fetch all the images from GridFS. When I run it for a single Id, it works. But when I try accessing from an array it throws error as "Cant set headers after they are sent". I am very new to mongodb. Please help me.