You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working with very simple file upload feature using Express.js & Multer.
This is my multer middleware code:
constpath=require('path')constmulter=require('multer')conststorage=multer.diskStorage({destination: function(req,file,cb){// Set the destination folder for uploaded filescb(null,'./public/uploads/')},filename: function(req,file,cb){// Generate a unique filename to avoid naming conflictsconstuniqueSuffix=Date.now()+'-'+Math.round(Math.random()*1e9)constoriginalFileExtension=path.extname(file.originalname)constfilename=file.fieldname+'-'+uniqueSuffix+originalFileExtensioncb(null,filename)},})constupload=multer({storage: storage,limits: {fileSize: 100*1024*1024},fileFilter: function(req,file,cb){constallowedTypes=/pdf|xls|zip|rar|jpg/constfileType=path.extname(file.originalname).toLowerCase()if(allowedTypes.test(fileType)===false){consterr=newError('File extensions is not allowed.')err.status=500err.name='FileValidationError'cb(err,false)}cb(null,true)},})module.exports=upload
When I hit this endpoint from Frontend (React.js) using Axios and FormData. I got never ending 'pending' request and I dont know why this is happen with this simple code. The expected results is custom JSON response should be returned.
After a minutes searching, I found that if I remove the 'FileValidationError' from error handler logic or using this code:
if(errinstanceofmulter.MulterError){}
The request is not pending and it returned response the Error() instance.
But, this behavior only happens in the Frontend side. I tested with Postman and the request looks fine.
Can someone tell me If I do something wrong in this situation?
The text was updated successfully, but these errors were encountered:
But, this behavior only happens in the Frontend side. I tested with Postman and the request looks fine.
@agussetyar I guess you need to add the header Accept: application/json in your frontend request in order to receive a JSON response
Already tried this approach, but also not working in my cases.
So far, I just return the default throw exception without any customization into JSON format.
I'm working with very simple file upload feature using Express.js & Multer.
This is my multer middleware code:
This is my error handler middleware:
When I hit this endpoint from Frontend (React.js) using Axios and FormData. I got never ending 'pending' request and I dont know why this is happen with this simple code. The expected results is custom JSON response should be returned.
After a minutes searching, I found that if I remove the 'FileValidationError' from error handler logic or using this code:
The request is not pending and it returned response the Error() instance.
But, this behavior only happens in the Frontend side. I tested with Postman and the request looks fine.
Can someone tell me If I do something wrong in this situation?
The text was updated successfully, but these errors were encountered: