【问题标题】:Error: "undefined" when uploading multiple files using multer错误:使用 multer 上传多个文件时出现“未定义”
【发布时间】:2022-03-04 10:19:51
【问题描述】:

路线:

router.post('/MOCReport', upload.array('files'), MOCReportController.MOCReport_post);

控制器:

exports.MOCReport_post = (req, res, next) => {      const files = req.files;
      console.log(files.filename);
      if(!files){
          const error = new Error('Please upload an image')
          error.httpStatusCode = 400
          return next (error)
      }
      var paths = req.files.map(files => file.path)
      let newMOCReport = new MOCReport({
        facilityName: req.body.facilityName,
        MoCDescription: req.body.MoCDescription,
        MoCReportDateTime: req.body.MoCReportDateTime,
        MoCDisasterLocation: req.body.MoCDisasterLocation,
        mocImage: req.files.paths
      });
      newMOCReport.save().then((MOCReportDoc) => {
        res.send(MOCReportDoc);
      });
    };
Schema:

    mocImage:{
            type: Array,
            required: false
        },

使用 multer 将多个图像上传到 mongoDB 数据库时出现未定义错误。

错误: 监听 3000 端口 连接MongoDB成功:)

未定义

POST /MoCReport 500 44.414 毫秒 - 43

【问题讨论】:

  • 请发布您遇到的确切错误。
  • 终端刚刚显示“未定义”,邮递员显示:文件未定义
  • 那听起来不像是错误。听起来你记录了一些东西,记录的项目的内容是undefined
  • 我不明白,它适用于单个图像上传,但是当更改为 upload.array 时它没有

标签: javascript node.js express mongoose multer


【解决方案1】:

检查您是否正在发送具有相同名称“files”的所有文件。

您可以尝试更改此设置:

upload.array('files')

到这里:

upload.any()

any() 方法将接受所有文件。它不会检查字段的名称。

此外,在此更改之后,您必须更改此行:

 var paths = req.files.map(files => file.path);

你有一个错误,因为你正在映射files并且你正在访问file。改成这样:

const paths = req.files.map(file => file.path);

【讨论】:

  • 它给出了同样的错误:"error": { "message": "file is not defined" }
  • 我正在使用邮递员进行测试,我会发图片
  • 等等,你的代码中没有file
  • 我更新了答案。现在可以试试了吗?
  • 它工作但提交时数组为空,我更新了图片以显示发送的响应是什么
【解决方案2】:

检查这是完整的工作代码。

 const express = require('express')
    const app = express()
    const port = 3000
    const multer  = require('multer')
    const upload = multer({ dest: 'uploads/' })
    
    
    app.get('/', (req, res) => {
        res.send('<form id="form_el" class=\'new-project\' action=\'/MOCReport\' method=\'POST\' enctype="multipart/form-data">\n' +
            '    <label for=\'file\'>Select your image:</label>\n' +
            '    <input type=\'file\' multiple=\'multiple\' accept=\'image/!*\' name=\'uploadedImages\' id=\'file\' />\n' +
            '    <span class=\'hint\'>Supported files: jpg, jpeg, png.</span>\n' +
            '    <button type=\'submit\'>upload</button>\n' +
            '</form>')
    })
    
    
    app.post('/MOCReport',upload.any(),function (req, res, next){
        const files = req.files;
        console.log(JSON.stringify(files));
    
        res.send('Uploaded successfully');
    });
    
    app.listen(port, () => {
        console.log(`Example app listening on port ${port}`)
    })

【讨论】:

    【解决方案3】:

    我也有类似的问题

    请查看反应代码

    请检查结构,查看formdata中是否正确加载了文件信息。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 2019-02-13
    • 2017-01-13
    • 1970-01-01
    相关资源
    最近更新 更多