我如何在邮递员中产生multer错误消息 [英] How do i produce multer error message in my postman

查看:106
本文介绍了我如何在邮递员中产生multer错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的文件图像超过1MB时,我试图向邮递员发送错误消息.但是,当我尝试运行我的代码时,它只会给我这整条错误消息.我只想获取错误消息本身(LIMIT_FILE_SIZE).有什么方法可以实现? 此处的图像

Im trying to res.status.send a multer error message to postman when my file image exceeds 1MB. But when i try to run my code it only gives me this entire chunk of error message. I just want to get the error message itself(LIMIT_FILE_SIZE).Is there any way to achieve this? IMAGE HERE

我当前的app.js:

My current 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})
        }
    }
    })


推荐答案

Multer将错误委托给Express,这是在express中抛出错误的标准方法.要捕获特定错误,可以在路由回调中使用multer上传中间件.这是multer的文档给出的方法,该方法也被@Mattia Rasulo提及

Multer delegates the error to Express which is the standard way of throwing errors in express. To catch a specific error, you can use the multer upload middleware inside the route callback. This is the method as given by multer's documentation, also mentioned by @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})
      }
    }
  });
});

这篇关于我如何在邮递员中产生multer错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆