I am storing an image in mongodb with BinData type.
I can query the database using mongojs with this.
db.images.findOne({
file_name: 'temp.jpg',
},
function(err, data){
console.log(data.image); // image buffer appears on the console
res.writeHead(200, {'Content-Type': 'image/jpg'});
res.end(data.image);
});
This produces "TypeError: first argument must be a string or Buffer".
I am pretty sure this has something to do with buffers or encoding. Can some please explain what I should be doing to the image-data before sending to the browser?
short example how i serve my files from mongodb gridfs
.getFileFromGridFs(doc,function(err, buffer){
if(err) return next(err)
res.setHeader('content-type',doc.mimeType)
res.setHeader('content-length',doc.size)
buffer.pipe(res)
})