Issue: The type '{ public_id: string; url: string; }[]' is missing several properties from the type 'DocumentArray<{ public_id: string; url: string; }>': isMongooseDocumentArray, create, id, $pop, and more.
The error occurs at product.photos = photosURL;
. However, it works fine when assigning values using
await Product.create({photos: photosURL});
export const updateProduct = TryCatch(async (req, res, next) => {
const { id } = req.params;
const { name, price, stock, category, description } = req.body;
const photos = req.files as Express.Multer.File[] | undefined;
const product = await Product.findById(id);
if (!product) return next(new ErrorHandler("Product Not Found", 404));
if (photos && photos.length > 0) {
const photosURL = await uploadToCloudinary(photos);
const ids = product.photos.map((photo) => photo.public_id);
await deleteFromCloudinary(ids);
product.photos = photosURL;
}
Current Product schema structure:
photos: [
{
public_id: {
type: String,
required: [true, "Please enter Public ID"],
},
url: {
type: String,
required: [true, "Please enter URL"],
},
},
],
const getBase64 = (file: Express.Multer.File) =>
`data:${file.mimetype};base64,${file.buffer.toString("base64")}`;
export const uploadToCloudinary = async (files: Express.Multer.File[]) => {
const promises = files.map(async (file) => {
return new Promise<UploadApiResponse>((resolve, reject) => {
cloudinary.uploader.upload(getBase64(file), (error, result) => {
if (error) return reject(error);
resolve(result!);
});
});
});
const result = await Promise.all(promises);
return result.map((i) => ({
public_id: i.public_id,
url: i.secure_url,
}));
};