iLiA
iLiA

Reputation: 3153

access request.body in multer filefilter

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!

Upvotes: 6

Views: 1871

Answers (1)

Trever Thompson
Trever Thompson

Reputation: 709

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

Upvotes: 9

Related Questions