快速 – multer fileFiltererror handling

我尝试上传一个文件,上传就OK了! 问题是,当我尝试上传一些不被我接受的文件fileFitler处理。 我想在控制台收到一些错误或将用户redirect到后台页面,但我只是在浏览器上收到错误:无法读取未定义的读取属性文件名。

但是,如果我上传一些被接受的扩展,就行了!

这是我的代码:

const mongoose = require('mongoose'); const multer = require('multer'); const uuidV4 = require('uuid/v4'); const Video = require('../models/videoModel'); function fileFilter(req, file, cb){ const extension = file.mimetype.split('/')[0]; if(extension !== 'video'){ return cb(null, false), new Error('Something went wrong'); } cb(null, true); }; var storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, './uploads') }, filename: function (req, file, cb) { const newName = uuidV4(); const extension = file.mimetype.split('/')[1]; cb(null, newName +`.${extension}`); } }); var upload = multer({ storage: storage, fileFilter: fileFilter}); exports.uploadVideo = upload.single('file'); // the single param should be the name of the file input form exports.CreateVideo = (req, res) => { const newVideo = {title: req.body.title, path: req.file.filename}; Video.create(newVideo, function(err, result){ if(err){ console.log('Não foi possível realizar o upload do vídeo.' + err); } else{ console.log('Vídeo upado com sucesso!'); res.redirect('/'); console.log(result); } }) }; exports.Home = (req, res) =>{ res.render('index'); }; exports.ShowVideos = (req, res) =>{ Video.find({}, function(err, result){ if(err){ res.redirect('/'); } else{ res.render('video', {videoData:result}); } }) }; 

Solutions Collecting From Web of "快速 – multer fileFiltererror handling"