0

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.',
 });
});

enter image description here

enter image description here

Can anyone helpe me resolve this one, please? Any help or suggestion is greatly appreacited. TIA.

oybek
  • 79
  • 1
  • 2
  • You may check out the issue as [described here in this document](https://stackoverflow.com/questions/60924989/typescript-this-expression-is-not-callable-type-getuserinforequestdata-ob). Additionally, here are some references on how to upload assets to your Cloudinary account [using MERN](https://betterprogramming.pub/image-upload-with-cloudinary-mern-f18812d5d023) and in [this document as well](https://betterprogramming.pub/how-to-upload-images-with-cloudinary-and-mern-part-2-83f8ee31b903) – epasos_573 Aug 07 '23 at 04:35
  • Thanks for your comment. I checked that issue and its discussion first and then posted my question here. I believe my issue is connected with File[] having no call signatures in ts, but it seems to be working fine in js. req.files has array functions but they are not callable. – oybek Aug 07 '23 at 07:53

0 Answers0