【问题标题】:TypeError: Cannot read property 'filename' of undefined--multer类型错误:无法读取未定义的属性“文件名”--multer
【发布时间】:2023-03-24 17:30:01
【问题描述】:

对于这个社区贡献者,我有一个非常相似的问题。 How do i produce multer error message in my postman其他用户做的cmet我也跟着翻了一遍,成功了!但是,当我尝试发布小于 1MB 并且是 jpg 格式的图像(我在编辑之前设法做到这一点)时,它现在失败并声明 TypeError: Cannot read property 'filename' of undefined

我的 app.js 代码:

const upload = multer({
    dest: storage,
    storage: storage,
    limits: {
      fileSize: 1024 * 1024
    },
    fileFilter: function(req, file, callback,error) {
        var ext = path.extname(file.originalname);
        var error_msg = error instanceof multer.MulterError;
        if(ext !== '.jpg') {
             req.fileValidationError = "Not a jpg file!";
             return callback(null, false, req.fileValidationError);
        }
        if(error_msg) {
            req.fileSizeError = "Image more than"
            return callback(null, false, req.fileSizeError)
        }
        callback(null,true)
    }
  });

app.post("/upload", function (req, res, next) {
    upload.single('name')(req, res, function (error) {
        if(req.fileValidationError) {
            res.status(500).send({message:req.fileValidationError});
        }
        else {
            if(error.code === 'LIMIT_FILE_SIZE') {
                req.fileSizeError = "Image more than 1MB!";
                res.status(500).send({message:req.fileSizeError});
            }
            else {
                console.log('File Received!');
                console.log(req.file);
                var sql = "INSERT INTO `file`(name,description,type,size) VALUES('" + req.file.filename + "', '" + (req.file.encoding + "_" + req.file.destination + "_" + req.file.path)+ "', '" + req.file.mimetype + "', '" + req.file.size + "')";
                db.query(sql, (error, results) => {
                    console.log('Inserted Data!');
                });
            const message = "Successfully Uploaded!"
            res.status(200).send({message:message, file_details:req.file})
            }
        }
    })
})

【问题讨论】:

    标签: mysql node.js express error-handling multer


    【解决方案1】:

    数组只包含对象,不包含文件名属性。数组中的第一个对象确实具有文件名属性。使用req.files['image'][0].filenamereq.files[0].filename 选择其中的第一个也是唯一一个项目,它应该可以工作。

    【讨论】:

      【解决方案2】:

      确保您在请求标头中发送"Content-Type": "multipart/form-data"

      【讨论】:

        【解决方案3】:

        似乎错误处理不正确,尤其是在文件保存期间;未处理由此产生的错误。比如尝试删除目标目录“uploads”然后上传文件,TypeError: Cannot read property 'filename' of undefined会再次抛出!

        要解决此问题并确定究竟是什么错误,您应该处理 upload.single() 错误回调。

        app.post("/upload", function (req, res, next) {
          upload.single('name')(req, res, function (error) {
            if (error) {
              console.log(`upload.single error: ${error}`);
              return res.sendStatus(500);
            }
            // code
          })
        });
        

        【讨论】:

          猜你喜欢
          • 2022-12-07
          • 2021-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-31
          • 1970-01-01
          • 2018-04-29
          • 1970-01-01
          相关资源
          最近更新 更多