access request.body in multer filefilter
Asked Answered
A

1

6

i want to do following: when user uploads file with form fields too check if form fields are empty, and if it is do not upload file. and i am using this code

fileFilter: (req,file,callback) =>{
     if(req.body.name.trim().length < 1){
         callback(null, false)
     } 
 }

but it gives me undefined for req.body.name and as i know fileFilter takes Express.Request as first argument and then why i can not access body?

full error

TypeError: Cannot read property 'trim' of undefined

Thanks!

Antipas answered 22/5, 2019 at 17:13 Comment(0)
D
9

This occurs when the client is sending the file before the fields. This can only be addressed client side. To resolve, switch the order in which the file and body properties are being appended to the uploaded object in the client.

More info: https://github.com/expressjs/multer/issues/299

Dichromate answered 1/7, 2019 at 23:58 Comment(1)
Its 3AM here and your answer saved me!Mansur

© 2022 - 2024 — McMap. All rights reserved.