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

217
Visualizações
GridFS TypeError: Cannot read property 'files' of undefined

I'm trying to upload and view files from MongoDB but while accessing files from MongoDB using GridFS raising an error "TypeError: Cannot read property 'files' of undefined". Anyone, please help me to figure out this error.

let gfs;
conn.once('open', function () {
    var gfs = Grid(conn.db, mongoose.mongo);
    gfs.collection('uploads');
});
app.get('/files',(req,res)=>{
    gfs.files.find().toArray((err,files)=>{
        if (err) return res.status(400).json({err});
        return res.json(files);
    }
    )
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I tried to figure out the error. I added the Grid object inside the app.get function. It worked!!

app.get('/files',(req,res)=>{
    var gfs = Grid(conn.db, mongoose.mongo);
    gfs.collection('uploads');
    gfs.files.find().toArray((err,files)=>{
        if (err) return res.status(400).json({err});
        return res.json(files);
    }
    )
});
about 4 years ago · Juan Pablo Isaza Relatório

0

In that code there are 2 distinct variables with the name gfs.

The variable in the outer/global scope declared with let gfs;, and the variable in the anonymous function declared with var gfs = ....

Inside the anonymous function, the local gfs masks the global variable, so the value set there is not visible outside of that anonymous function.

about 4 years ago · Juan Pablo Isaza Relatório
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