Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
GridFS: TypeError: grid.mongo.ObjectID is not a constructor at createReadStream

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!