I am trying to get documents (images/pdf) stored in MongoDB, After receving few documents I get this error
MongoError file with id ### not opened for writing
So far I have tried multiple changes but none is working, following is the code for getting the documents,
async getFilesData(res, bucketName, photo) {
gfs.collection(bucketName);
let filename = photo;
gfs.files.findOne({ filename }, (err, file) => {
// Check if file
if (!file || file.length === 0) {
return res.status(404).json({
err: 'No file exists',
});
}
const readstream = gfs.createReadStream(file.filename);
res.set('Content-Type', file.contentType);
return readstream.pipe(res);
// Check if image
/* if (file.contentType === 'image/jpeg' || file.contentType === 'image/png') {
// Read output to browser
const readstream = gfs.createReadStream(file.filename);
res.set('Content-Type', file.contentType);
return readstream.pipe(res);
} else {
res.status(404).json({
err: 'Not an image'
});
} */
});
}```