I'm trying to upload a video using multer, I have a form and everything but when I click create, validator gives an error however, the file is saved in my directory as if validation was successful. I'm using mongoDB for database.
here's the validator
const { check } = require('express-validator/check'); const Video = require('app/models/VideoModel'); const path = require('path'); class videoValidator extends validator { handle() { return [ check('title') .isLength({ min : 5 }) .withMessage('title should be at least 5 characters'), check('artist') .not().isEmpty() .withMessage('artist field can not be empty'), check('videos') .custom(async (value , { req }) => { if(req.query._method === 'put' && value === undefined) return; if(! value) throw new Error('the video field can not remain empty'); let fileExt = ['.webm' , '.ogg' , '.mp4' , '.avi']; if(! fileExt.includes(path.extname(value))) throw new Error('file extention is not supported') }), } slug(title) { return title.replace(/([^?-??-?a-z0-9]|-)+/g , "-") } } module.exports = new videoValidator();
I just realized I've forgotten to add convertFileToField middleware. it's working fine now
handle(req , res , next) { if(! req.file) req.body.videos = undefined; else req.body.videos = req.file.filename; next(); }
2.1m questions
2.1m answers
60 comments
57.0k users