【问题标题】:How do i produce multer error message in my postman我如何在邮递员中生成 multer 错误消息
【发布时间】:2020-04-18 22:14:24
【问题描述】:

当我的文件图像超过 1MB 时,我试图 res.status.send 向邮递员发送 multer 错误消息。但是当我尝试运行我的代码时,它只会给我这整块错误消息。我只想获取错误消息本身(LIMIT_FILE_SIZE)。有没有办法实现这一点? IMAGE HERE

我当前的 app.js:

var multer = require('multer');
var storage = multer.diskStorage({
    destination: function(req, file, callback) {
        callback(null, './uploads');
    },
    filename: function(req, file, callback) {
        callback(null, path.basename(file.originalname));
    }
})
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) {
            return callback(null, false, new MulterError('LIMIT_FILE_SIZE'))
        }
        callback(null,true)
    }


  });
app.post("/upload",upload.single('name'),(req,res,next) => {
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】:

    Multer 只是将错误发送到您的全局错误中间件,因此您只需捕获它并检查错误是什么:

    if(err.message === 'file too large') [change the message as you need].
    

    这就是我处理您完全相同的问题的方式!

    https://www.npmjs.com/package/multer#error-handling

    【讨论】:

      【解决方案2】:

      Multer 将错误委托给 Express,这是在 express 中抛出错误的标准方式。要捕获特定错误,您可以在路由回调中使用 multer upload 中间件。这是 multer 的documentation 给出的方法,@Mattia Rasulo 也提到过

      router.post('/image', function (req, res, next) {
        upload.single('image')(req, res, function (error) {
          if (req.fileValidationError) {
            res.status(500).send({ message: req.fileValidationError });
          }
          else {
            if (error) {
              res.status(500).send({ message: error.code === 'LIMIT_FILE_SIZE' ? "Image more than 1MB!" : error.message });
            }
            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})
            }
          }
        });
      });
      

      【讨论】:

      • 谢谢你的回答详细多了,我有点懒! ?
      • ? 我能理解,有时我也能理解
      • 谢谢,这是一个很大的帮助。它帮助解决了我遇到的问题!
      • 但是,解决问题后,当我输入正确的文件时,我的成功消息没有显示。有没有办法解决这个问题?
      • @TristonLoh 在成功的情况下未定义error 对象时代码失败。在答案中修复它
      猜你喜欢
      • 1970-01-01
      • 2020-07-15
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 2019-07-21
      • 2013-06-30
      • 2021-12-11
      • 1970-01-01
      相关资源
      最近更新 更多