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

175
Views
GridFS mongodb & nodejs req.file is undifine

I'm trying to upload files into mongodb using multer gridFS. I have a problem on server - req.file is undefine. Using upload.single('file') which should give me href on my client side of my input.

I do get the href of the file I chose on client , but unfortunately don't create this on my DB.

GridFS middleware :

const crypto = require('crypto')
const multer = require('multer')
const {GridFsStorage} = require('multer-gridfs-storage')
const path = require('path')


const storage = new GridFsStorage({
    url: process.env.DB_CONNECT,
    file: (req, file) => {
      return new Promise((resolve, reject) => {
        crypto.randomBytes(16, (err, buf) => {
          if (err) {
            return reject(err);
          }
          const filename = buf.toString('hex') + path.extname(file.originalname);
          const fileInfo = {
            filename: filename,
            bucketName: 'uploads'
          };
          resolve(fileInfo);
        });
      });
    }
  });
  module.exports = multer({ storage: storage });

Route - uploadFile error don't recognize req.file from upload.single('file'):

const router = require('express').Router();
const FileController = require('../controllers/FileController')
const upload = require('../middleware/GridFS')
//upload new file

router.post('/uploadFile', upload.single('file') , (req,res) =>{
  console.log(req.file) // not define
  //send to mongodb
})
// update exist file
router.post('/updateFile', FileController.updateFile)

module.exports = router;
about 4 years ago · Santiago Trujillo
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!