I am developing a platform where users should be able to create a hotel via an HTML form. Inside the form, there is an option to upload multiple image files.
I am using multer to handle the upload. My procedure is the following:
- Save uploaded images in 'images' directory
- convert images in 'images' directory to base64
- save base64-format in an array
- save array in Hotel MongoDB Schema
- delete directory 'images'
The upload and saving work perfectly but I get an error when I try to upload the next hotel:
Error: ENOENT: no such file or directory, open 'C:\ ...'
The images are successfully stored in the 'images' directory, but obviously, as the app crashes, nothing is done with the files so I have to delete them manually.
As this is only happening on the second try (when restarting the application, it works again) I assume that fs.readFileSync
is executed before the images are done uploading. I am clueless as multer is part of my router chain, the conversion to base64 should happen AFTER all files are uploaded to the server.
What am I doing wrong?
Multer functions: https://i.stack.imgur.com/Wz167.png Base64 conversion: https://i.stack.imgur.com/oOEmR.png Delete function (executed after saving to DB) https://i.stack.imgur.com/PlTvH.png
