let storage = multer.diskStorage({
destination: (req, file, cb) => {
if (process.env.DEVELOPMENT === 'dev') {
return cb(null, `${process.env.DEV_PATH_TO_PHOTO_FOLDER}`)
}
return cb(null, `${process.env.PROD_PATH_TO_PROTO_FOLDER}`)
},
filename: (req, file, cb) => {
return cb(null, `${user_id}.jpeg`)
}
});
let upload = multer({
fileFilter: async (req, file, cb) => {
if (file) {
if (file.originalname.match(/\.(jpg)$/)) {
return cb(null, file)
} else if (file.originalname.match(/\.(jpeg)$/)) {
cb(null, file)
}
cb(new Error('file_type'));
}
cb(new Error('file is not passed'));
},
storage: storage
}).single('photo');
return new Promise((resolve, reject) => {
return upload(req, res, (err) => {
console.log(err, 'error')
if (err) return reject(new Error(err));
if (req.file) {
return resolve(`http://${process.env.HOST_FOR_MEDIA}:3000/photo/${user_id}.jpeg`)
}
return reject(new Error('File format is not correct'))
})
})
你可以在我的代码中看到我想在文件过滤器中进行错误处理..并获取有关此方法中出现的错误的数据..
return upload(req, res, (err) => {
console.log(err, 'error')
if (err) return reject(new Error(err));
if (req.file) {
return resolve(`http://${process.env.HOST_FOR_MEDIA}:3000/photo/${user_id}.jpeg`)
}
return reject(new Error('File format is not correct'))
})
但是当我尝试制作console.log(error)
时,我得到了不确定。我也试图像req.error = 'error_text'
那样制作smth,但它也给了我
未定义
我犯了什么错误以及如何在上面提到的方法中得到错误?