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

203
Views
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 answers
Answer question

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 Report

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 Report
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!