无法将文件上传到MongoDb

时间:2019-09-05 15:46:55

标签: node.js mongodb express multer multer-gridfs-storage

我正在尝试使用multer和multer-grid-fs-storage将.zip文件上传到MongoDb Atlas。

它可以正常编译,但是什么也没有上传到我的集群中。

我曾尝试将文件保存到磁盘存储中,然后直接发送到MongoDB,但都无法正常工作。

我正在测试失眠症。请求状态为200(正常),但是未上传任何内容。当我console.log req.file时,它表示未定义;当我登录req.body时,它记录一个空对象(即{})。

  • 这是我的index.js文件,要求并使用路由和数据库连接:
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)
  • 这是我的文件控制器模块:
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
}
  • 这是我的路由器
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;
  • 这是我的MongoDb连接模块
const mongoose = require('mongoose')

mongoose
    .connect('mongodb+srv://Nancelot:Archer212@cluster0-3d7n6.gcp.mongodb.net/test?retryWrites=true&w=majority', { useNewUrlParser: true })
    .catch(e => {
        console.error('Connection error', e.message)
    })

const db = mongoose.connection

module.exports = db

注释掉的存储器存储对象是我也尝试过的磁盘存储。都没用

1 个答案:

答案 0 :(得分:1)

我不确定,这可能是您的问题所在,实际上对我有用。我使用“文件”键在正文中发送.csv文件。 但是我不得不重写您的get函数,以取回那些文件。

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);
    });
};

...并且我建议像这样等待mongo连接打开(这可能是个问题) 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);