Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda