How to submit form with Multer with optional file submission?
One workaround of this is to check the value of req.file
once your submission goes through. If you do not provide a file in your form, req.file
should have the value of undefined
. However, if you submit a file it should be an object
. Therefore, you could write a simple if
statement like so:
function Create() { if (req.file !== undefined) { // process image here } // process all other fields}
Hope it helps!