I am working on a MERN full-stack app and using Express, Multer, and Cloudinary for image upload. I set the image upload form as encType='multipart/form-data' and added multiple prop to the input in front-end. In the backend, I am trying to map over the req.files but it is giving me the below type error, and I cannot call any other array functions (e.g. filter, every, some, etc.) except length. I am not sure if I am doing something wrong or missing anything. When I log req.files to the console, it is an array of File and the uploaded images are logged fine, without any errors.
export const createCampground = asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
const { title, location, price, description } = req.body;
const images: Array<{ url: string; filename: string }> = req.files?.map(
(file) => ({ url: file.path, filename: file.filename })
);
console.log(req.files)
const newCampground = new Campground({ ...req.body });
await newCampground.save();
res.json({
message: 'Campground created successfully.',
});
});
Can anyone helpe me resolve this one, please? Any help or suggestion is greatly appreacited. TIA.