Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda