mysql - 我如何在我的 postman 中产生多个错误消息

标签 mysql node.js express error-handling multer

当我的文件图像超过 1MB 时,我正在尝试res.status.send 向 postman 发送一条错误消息。但是当我尝试运行我的代码时,它只会给我整 block 错误消息。我只想获取错误消息本身(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})
        }
    }
    })


最佳答案

Multer 将错误委托(delegate)给 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})
      }
    }
  });
});

关于mysql - 我如何在我的 postman 中产生多个错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59526207/

相关文章:

mysql - 无法弄清楚我在双重否定查询(mysql)中出了什么问题

php - 将具有 unix 时间戳值的 PHP 字符串转换为 MySQL 时间戳值

php - 如何根据相同的日期对几个时间戳进行分类?

windows - 安装包后找不到 Git Bash 命令

node.js - 如何使用 node.js 在 Windows Azure 上修复 "Port 80 requires elevated privileges"?

javascript - 如何在 node.js 应用程序中正确访问由 express 设置的应用程序变量?

python - 什么是用一个键和多个值更改字典以获得所需输出的 ​​Pythonic 方法?

javascript - Node : SyntaxError: Unexpected identifier

javascript - 在 Sails Express js 中,除了异步处理之外,AJAX/AJAJ 的意义何在?

node.js - Angular Universal 与 api 内部快速调用