I am creating a Real estate server and I have created a small form to test my endpoint. now I sending the images back to the server as an array after they have been encoded to base64. I am checking first if the req.body.image.isArray then proceeding to upload however I think I am getting it wrong in the function since it takes two arguments, the item uploaded and the upload preset. I am trying to send every image in the array by looping through but it seems I receive the 404 status. How do I loop through each image in this array and get back the uploadResponse for each image?
My Code.
Router.post("/createproduct", async (req, res) => {
const { error } = productValidation.validate(req.body);
if (error) {
return res.status(400).json({ message: error.details[0].message });
}
try {
if (req.body.image.isArray) {
const uploadRes = await cloudinary.uploader.upload(
req.body.image.array.forEach((img) => {
return img;
}),
{
upload_preset: "fanakaplatinums",
}
);
if (uploadRes) {
await productsModel.create({
name: req.body.name,
location: req.body.location,
description: req.body.description,
price: req.body.price,
amenities: req.body.amenities,
image: uploadRes,
});
return;
}
} else {
res.status(400).json({ message: "an error occured" });
}
res
.status(200)
.json({ message: "the house was added to database successfully" });
} catch (error) {
res.status(500).json({ message: error});
}
});