Multer Unexpected field, even field names are same

2019-08-19 05:00发布

问题:

I am uploading multiple files from different fields. when i upload file via Postman, Multer fails with Unexpected field error. even though i checked, my field names are exactly same with which i am using in multer.

This is my exception:

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)

This is my multer middleware

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}])

Here is my Postman

回答1:

I was banging around my head, and finally found a mistake/type

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

{name: 'idCardPhotoBackPath`', maxCount: 1} <=== file name has this back tick