W
W
WapSter2020-08-30 19:08:58
JavaScript
WapSter, 2020-08-30 19:08:58

Get req.body from multipart/form-data?

There is a mixed form in which there are fields with text information and a field for files. The essence of the problem is that I need to form the path for the files according to the information of one of the fields. But I can't get access to this information (I'm getting it too late)

router.post('/add', async (req, res) => {
    const storage = multer.diskStorage({
        destination(req, file, cb) {
            console.log(req.body) // undefined
            cb(null, 'storage/')
        },
        filename(req, file, cb) {
            console.log(req.body) // undefined
            cb(null, file.originalname)
        }
    })
    const upload = multer({
        storage
    }).fields([{name: 'files'}, {name: 'title'}])
    upload(req, res, (err) => {
        console.log(req.body) // сформированный объект с title
    })
})

How can req.body be accessed in the destination or filename functions?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question