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

Rapid
  • 111
  • 2
  • 13
  • 1
    Looks like your ending the request after you have the first image with res.writeHead(200, {'Content-Type': 'image/png'});. After you have sent the response headers you can't send them again in that same request. – Ross Aug 26 '15 at 10:42
  • Hi @Mac, can I send multiple response to the same request? – Rapid Aug 26 '15 at 11:35
  • I do have to ask "why" would you want to send the data for multiple images in the same response? Remember that this is just a raw byte stream being sent, so it is just like concatenating the image data together into a single file. If the code was correct that is. – Blakes Seven Aug 26 '15 at 11:52

0 Answers0