1

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.