2

I want to Use multer function in controller file But the problem is req.body got undefine when I use postman form-data body to upload images This is link to code that how I am using multer Function but I want use it in my controller

how I want to use it as you can see in below code

const multerHelper = require("../helpers/multer_helper");

Documents: async (req, res) => {
    console.log(req.body)
    if (!req.body.id) {
        console.log(req.body)
        logger.warn(error.MANDATORY_FIELDS);
        return res.status(500).send(error.MANDATORY_FIELDS)
    }

    try {
        multerHelper.createUserImage
        let result = error.OK
        logger.info(result);
        return res.status(200).send(result)

    } catch (err) {
        logger.warn(err);
        console.log(err);
        return res.status(500).send(error.SERVER_ERROR)
    }
}

but it throws error like req.body undefine

req.body empty image enter image description here

postman form-data image

enter image description here

Anyone know how to do it

2 Answers 2

2

You can use multer functions and objects in only routes not in controller if you want to use it in controller you have to write storage and upload functions in controllers, here I have used multer error handling and I am uploading multiple images

Documents: async (req, res) => {

        if (!req.headers.room_id) {
            logger.warn(error.MANDATORY_FIELDS);
            return res.status(500).send(error.MANDATORY_FIELDS)
        }

        try {
            let storage = multer.diskStorage({
                destination: function (req, file, cb) {
                    let id = req.headers.room_id;
                    let path = `tmp/daily_gasoline_report/${id}`;
                    fsextra.mkdirsSync(path);
                    cb(null, path);
                },
                filename: function (req, file, cb) {
                    // console.log(file);
            
                    let extArray = file.mimetype.split("/");
                    let extension = extArray[extArray.length - 1];
                    cb(null, file.fieldname + '-' + Date.now() + "." + extension);
                }
            })
            var upload = multer({ storage: storage }).array('images', 100);
            upload(req, res, function (err) {
                if (err) {
                    console.log(err);
                    return res.end("Error uploading file.");
                } else {
                    res.end("File has been uploaded");
                }
            });
            let result = error.OK
            logger.info(result);
            return res.status(200).send(result)

        } catch (err) {
            logger.warn(err);
            console.log(err);
            return res.status(500).send(error.SERVER_ERROR)
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

2

Do you have a parser? How do you parse the data? Your gonna need to use something that gives you this data something like: https://www.npmjs.com/package/express-fileupload

this package helps you to parse the form data and the files data itself.

2 Comments

i am using multer for that like this stackoverflow.com/a/64117357/12761193 but now i want to use it in controller not in route
You need to understand what multer is going, its parsing the files data. This express-fileupload is parsing the data and put it on the req.files And then you will be able to use it on the endpoint

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.