I am trying to upload an image file in an express endpoint using multer but it is not working. I keep getting the 500 unexpected field error. I need to be able to read the image from the route
const router = require('express').Router();
const multer = require('multer');
const path = require('path');
const storage = multer.diskStorage({
destination: './public/uploads/images',
filename: function (req, file, cb) {
cb(null, file.fieldname '-' Date.now()
path.extname(file.originalname));
}
});
const upload = multer({ storage: storage, limits: { fieldSize: 10 * 1024 * 1024 } });
/**
* @swagger
* /api/items/test:
* post:
* summary: add a new item
* tags: [Item]
* description: use to add a new item to a specific vendor
* requestBody:
* content:
* multipart/form-data:
* schema:
* type: object
* properties:
* fileName:
* type: string
* format: binary
*/
router.post('/test', upload.single('featuredImage'), async (req, res) => {
try {
console.log(req.file)
} catch (error) {
res.send(error.message);
}
});
CodePudding user response:
fileName
should correspond to the field name given in multer
reference, i.e. featuredImage
, hence the error
try replacing it:
* properties:
* featuredImage:
* type: string
* format: binary