New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error handling not working #336
Comments
Ok, I have managed to figure out I can change the code to something like:
Now it gets triggered, everything works fine, but when trying to upload an invalid file ( pdf ) which is filtered in Additionally I have added |
Did you ever get this fixed? Having a heck of a time trying get get error handling on fileFilter working |
Hi @wesbos I came up with the following code, which works just fine for me. var multer = multer({
dest: App.uploadsDir,
limits: { fileSize: App.maxMediaFileSize },
fileFilter: ( req, file, cb ) => {
console.log( file.mimetype );
cb( null, file.mimetype == 'image/jpeg' )
}
}),
avatarUpload = multer.single('avatar');
router.post( '/avatar', avatarUpload, ( req, res ) => {
avatarUpload( req, res, ( err ) => {
if ( err || !req.file )
return res.send({ error: 'invalid_file' })
console.log( 'save the file', req.file );
});
}); Still, the Hope it helps ( using |
Sorry for not commenting on this earlier. The last example is really good, the reason the fileFilter thing isn't showing up as an error is because it isn't intended to. If you want to trigger an error, you can supply it to the callback, so something like this: var multer = require('multer')({
dest: App.uploadsDir,
limits: { fileSize: App.maxMediaFileSize },
fileFilter: (req, file, cb) => {
if (file.mimetype !== 'image/jpeg') {
return cb(new Error('Only jpeg images allowed'))
}
cb(null, true)
}
})
var avatarUpload = multer.single('avatar')
router.post('/avatar', (req, res) => {
avatarUpload(req, res, (err) => {
if (err) return res.send({ error: 'invalid_file' })
console.log('save the file', req.file)
})
}) also, you shouldn't call the I hope this clears everything up, feel free to reopen if you have more problems :) |
im still getting undefined :/ |
im still getting undefined :/ 2024 and im having same problem. if you mean you get undefined instead of the json, this can help youMaybe multer have a bug in a callback function when error is passed. In your case res.json({error:"invalid format"}); this way the file isnt save it and the server send the json with error if the mimetype is correct execute cb(null,true); finally in the avatarUpoad function NOT validate an error, just send a json without error like this
|
Hello ! I want to be able to handle the
limits
and thefileFilter
errors and I'm doing just as the doc says:My code looks like this
Bare in mind that it works perfectly if I remove the inner
upload.single()
and I add it as middleware:But then, the downside is I can't get the error anymore.
Any solution for this ? The
uploading
log gets triggered correctly, but it doesn't fire the upload if not set as middleware.The text was updated successfully, but these errors were encountered: