Can't get file from GridFS. Here is my code:
const mongoose = require('mongoose');
const Grid = require('gridfs-stream');
const connect = mongoose.connection;
let gfs;
connect.once('open', () => {
gfs = new Grid(connect.db, mongoose.mongo);
gfs.collection('avatars');
});
router.get('/:filename', async (req, res) => {
try {
const file = await gfs.files.findOne({ filename: req.params.filename });
const readStream = gfs.createReadStream({ filename: file.filename }); // at this moment I get error
readStream.pipe(res);
} catch (err) {
console.log(err);
res.status(400).json({ message: 'Not Found' });
}
});
The file is found, everything is ok, but when creating a read stream, it gives the following error:
TypeError: grid.mongo.ObjectID is not a constructor
at new GridReadStream (C:\Users\karrt\Documents\sneaky\fullstack-login-template\server\node_modules\gridfs-stream\lib\readstream.js:68:62)
at Grid.createReadStream (C:\Users\karrt\Documents\sneaky\fullstack-login-template\server\node_modules\gridfs-stream\lib\index.js:53:10)
at C:\Users\karrt\Documents\sneaky\fullstack-login-template\server\routes\avatar.routes.js:20:28
at processTicksAndRejections (internal/process/task_queues.js:95:5)
Please tell me what could be the problem, by the way, here is the file that returns gfs.files.findOne
{
_id: new ObjectId("6144c3a2323799ae2eefa93c"),
length: 121123,
chunkSize: 261120,
uploadDate: 2021-09-17T16:34:42.865Z,
filename: 'f9e9eb2d-fc2b-408c-a878-45eb6c6ae9e8-Screenshot-2021-09-14-061726.png',
contentType: 'image/png'
}
new ObjectId returns mongo for me, but in the Grid constructor, I indicated that I am using mongo...