Multer意外字段,即使字段名称相同

时间:2019-04-16 21:20:12

标签: node.js multer

我要从不同的字段上传多个文件。 当我通过邮递员上传文件时,Multer失败,出现意外字段错误。即使我进行了检查,我的字段名称也与我在multer中使用的字段名称完全相同。

这是我的例外:

MulterError: Unexpected field
at wrappedFileFilter (F:\Node-Installments-Management\node_modules\multer\index.js:40:19)
at Busboy.<anonymous> (F:\Node-Installments-Management\node_modules\multer\lib\make-middleware.js:114:7)
at Busboy.emit (events.js:182:13)
at Busboy.emit (F:\Node-Installments-Management\node_modules\busboy\lib\main.js:38:33)
at PartStream.<anonymous> (F:\Node-Installments-Management\node_modules\busboy\lib\types\multipart.js:213:13)
at PartStream.emit (events.js:182:13)
at HeaderParser.<anonymous> (F:\Node-Installments-Management\node_modules\dicer\lib\Dicer.js:51:16)
at HeaderParser.emit (events.js:182:13)
at HeaderParser._finish (F:\Node-Installments-Management\node_modules\dicer\lib\HeaderParser.js:68:8)
at SBMH.<anonymous> (F:\Node-Installments-Management\node_modules\dicer\lib\HeaderParser.js:40:12)
at SBMH.emit (events.js:182:13)
at SBMH._sbmh_feed (F:\Node-Installments-Management\node_modules\streamsearch\lib\sbmh.js:159:14)
at SBMH.push (F:\Node-Installments-Management\node_modules\streamsearch\lib\sbmh.js:56:14)
at HeaderParser.push (F:\Node-Installments-Management\node_modules\dicer\lib\HeaderParser.js:46:19)
at Dicer._oninfo (F:\Node-Installments-Management\node_modules\dicer\lib\Dicer.js:197:25)
at SBMH.<anonymous> (F:\Node-Installments-Management\node_modules\dicer\lib\Dicer.js:127:10)

这是我的multer中间件

const multer = require('multer');

    const MIME_TYPE_MAP = {
        'image/png': 'png',
        'image/jpeg': 'jpg',
        'image/jpg': 'jpg'
    };

    const storage = multer.diskStorage({
        destination: (req, file, cb) => {
            const isValid = MIME_TYPE_MAP[file.mimetype];
            let error = new Error('Invalid mime type');
            if (isValid) {
                error = null;
            }
            cb(error, req.uploadPath);
        },
        filename: (req, file, cb) => {
            const name = file.originalname.toLocaleLowerCase().split(' ').join('-');
            const ext = MIME_TYPE_MAP[file.mimetype];
            cb(null, name + '-', + Date.now() + '.' + ext);
        }
    });

    module.exports = multer({storage: storage}).fields([{name: 'idCardPhotoFrontPath', maxCount: 1}, {name: 'idCardPhotoBackPath`', maxCount: 1}])

这是我的邮递员

{{3}}

1 个答案:

答案 0 :(得分:0)

我在敲打头,最后发现一个错误/类型

  

module.exports = multer({storage:storage})。fields([{name:   'idCardPhotoFrontPath',maxCount:1},{名称:'idCardPhotoBackPath`',   maxCount:1}]);

{名称:“ idCardPhotoBackPath`”,maxCount:1} <===文件名带有反勾