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

265
Views
Can not get files metadata from mongodb as gfs of Gridfs-stream is undefined

I can upload files to mongodb using the standard Gridfs-stream implementation through a POST request. But, when I need to view the file metadata through GET request I'm getting error:

TypeError: Cannot read property 'files' of undefined

Apparently, it's not recognising my definition of gfs.This is my route handler:

router.get('/image/:filename', (req, res) => {

  gfs.files.find({filename: req.params.filename}, function(err, file) {
    let readStream = gfs.createReadStream({filename: file.filename});
    let data = '';
    readStream.on('data', (chunk) => {
      data += chunk.toString('base64');
    })
    readStream.on("error", function (err) {
      res.send("Image not found");
    });
    readStream.pipe(res);
  })

  res.json();
});

I have defined gridfs-stream in the simplest way:

const conn = mongoose.createConnection(URI, {useNewUrlParser:true, useUnifiedTopology:true});

let gfs;
Grid.mongo = mongoose.mongo;
conn.once('open', () => {
    gfs = Grid(conn.db);
    gfs.collection('uploads');
});

const storage = new GridFsStorage({
    url: URI,
    file: (req, file) => {...}
});

const upload = multer({ storage });

I can't understand what is the problem here, I'm stuck for days!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try using this:

app.get("/file/:filename", async (req, res) => {
    try {
        const file = await gfs.files.findOne({ filename: req.params.filename });
        const readStream = gfs.createReadStream(file.filename);
        readStream.pipe(res);
    } catch (error) {
        res.send("not found");
    }
});

over 4 years ago · Santiago Trujillo 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!