I want Multer allows just photo to be uploaded but it doesn't work and gives request.file = undefined and at the same time the file.mimetype = image/jpeg which is correct
I checked the headers also and it contains Content-Type = multipart/form-data; boundary=<calculated when request is sent>
then I tried and removed fileFilter: multerFilter
configurations from Multer and the surprise that everything worked fine without any single problem. Here is the related code:
const multerStorage = multer.memoryStorage();
// 2) Creating a multer filter (to test if the uploaded file is really an image):
const multerFilter = (request, file, cb) => {
if (file.mimetype.startsWith("image")) cb(null, true);
cb(new AppError(400, `The only accepted files are images, please select an image ${request.file} file tyep: ${file.mimetype}`), false);
};
const upload = multer({ storage: multerStorage, fileFilter: multerFilter });
exports.updateMyPhoto = upload.single("photo");
Please help me find what's wrong with my fileFilter configuration
UPDATE:
I switched between the two conditions inside multerFilter
and the program is working perfectly now. the new code:
const multerFilter = (request, file, cb) => {
if (!file.mimetype.startsWith("image")) cb(new AppError(400, `The only accepted files are images, please select an image ${request.file} file tyep: ${file.mimetype}`), false);
cb(null, true);
};
I still don't know the reason for this behaviour so I'd appreciate your help