在multer.none()和multer.any()回调内部无法上传多文件。
我试图在上传到服务器之前从mongodb生成文件名。生成文件名multer文件后,无法在multer.none()和multer.any()内部上载文件。
const doUpload = (testimonial)=>{
const upload = multer({
storage: multer.diskStorage({
destination: (req, file, cb) => cb(null, 'public/uploads/company/testimonials'),
filename: (req, file, cb) => cb(null,`${testimonial._id}.jpg`)
}),
fileFilter: (req, file, cb) =>['image/jpeg','image/jpg','image/png'].includes(file.mimetype) ? cb(null, true) : cb(new Error(RESPONSE.UPLOAD_ERROR))
});
upload.single('userAvatar')(req, res, function (err){
if(err)
res.json(RESPONSE.UPLOAD_ERROR);
else{
res.json(Object.assign({},RESPONSE.SAVED,{ redirect: USER.COMPANY }));
}
});
};
multer().none()(req, res, function(){
let data = req.body;
Company.findOneAndUpdate({ user_id: req.user._id},{
$push: {
testimonials: {
userName: data.userName,
organizationName: data.organizationName,
testimonialDate: data.testimonialDate,
testimonial: data.testimonial
}
}
},{
runValidators: true,
new: true
}).then(company=>{
doUpload(company.testimonials[company.testimonials.length-1]);
});
});