0
votes

I am trying to upload a .zip file to MongoDb Atlas with multer and multer-grid-fs-storage.

It compiles fine, however nothing is being uploaded to my cluster.

I have tried saving the file to disk storage and also sending straight to MongoDB, neither are working.

I am testing with Insomnia. The request status is 200 (ok), however nothing is being uploaded. When I console.log req.file it says undefined, and when I log req.body it logs an empty object (i.e. {}).

  • Here is my index.js file requiring and using the routes and db connection:
const db = require('./db/index.js')
db.on('error', console.error.bind(console, 'MongoDB connection error:'))

require('dotenv').config();
const app = express();


const userRouter = require('./routes/user-routes')
app.use('/api', userRouter)

const fileRouter = require('./routes/file-routes')
app.use('/file', fileRouter)
  • Here is my file controller module:
const db = require('../db/index.js')
const fs = require('fs');
const path = require('path');
const multer = require('multer');
const GridFsStorage = require('multer-gridfs-storage') 

const storage = new GridFsStorage({
    db: db,
    file: (req, file) => {
      return {
        filename: file.originalname
      }
    }
  });

// const storage = multer.diskStorage({
//   destination: (req, file, cb) => {
//     const uploadDir= path.join (__dirname, "..", "uploads", "zip" `${Date.now()}`)
//     // fs.mkdirSync(zip)
//     cd (null, uploadDir)
//   }, 
//   filename: (req, file, cb) => {
//     cb(null, file.originalname)
//   }
// })

  // sets file input to single file
const singleUpload = multer({ storage: storage }).single('file');

 const getFiles = (req, res) => {
    gfs.files.find({ filename: req.params.filename }).toArray((err, files) => {
      if(!files || files.length === 0){
        return res.status(404).json({
          message: "Could not find file"
        });
      }
      var readstream = gfs.createReadStream({
        filename: files[0].filename
      })
      res.set('Content-Type', files[0].contentType);
      return readstream.pipe(res);
    });
};

const postFiles = (req, res) => {
  res.send({ success: true });
  console.log(req.file)
};

module.exports = {
  storage, 
  singleUpload,
  getFiles, 
  postFiles,
  // getFiles2, 
  // deleteFiles
}
  • Here is my router
const express = require('express')

const FileCtrl = require('../controllers/file-ctrl')

const router = express.Router()

router.get('/getFiles', FileCtrl.getFiles)
router.get('/getFiles2', FileCtrl.getFiles2)
router.post('/postFiles', FileCtrl.singleUpload, FileCtrl.postFiles)
router.post('/deleteFiles', FileCtrl.deleteFiles)

module.exports = router;
  • Here is my MongoDb connection module
const mongoose = require('mongoose')

mongoose
    .connect('mongodb+srv://Nancelot:[email protected]/test?retryWrites=true&w=majority', { useNewUrlParser: true })
    .catch(e => {
        console.error('Connection error', e.message)
    })

const db = mongoose.connection

module.exports = db

The commented out multer storage object is the disk storage I also tried. Neither are working

1

1 Answers

1
votes

I'm not sure, what could be the problem on your side, It's working for me actually. I send in a .csv file in the body, with a 'file' key. But I had to rewrite your get function, to get those files back.

const getFiles = (req, res) => {
    const gfs = new mongoose.mongo.GridFSBucket(mongoose.connections[0].db);
    gfs.find({}).toArray((err, files) => {
        if (!files || files.length === 0) {
            return res.status(404).json({
                message: 'Could not find file',
            });
        }
        var readstream = gfs.openDownloadStream(files[0]._id);
        res.set('Content-Type', files[0].contentType);
        return readstream.pipe(res);
    });
};

...and I recommend to wait for the mongo connection to be open, like this (this might be a problem) app.js:

const express = require('express');
const db = require('./db/index.js');
const mongoose = require('mongoose');
db.on('error', console.error.bind(console, 'MongoDB connection error:'));

require('dotenv').config();
const app = express();
const run = () =>
    app.listen(3000, () => {
        console.log(`Server running on http://localhost:3000/`);
    });

const fileRouter = require('./routes/file-routes.js');
app.use('/file', fileRouter);
mongoose.connection.once('open', run);