I'm trying to set images after uploading them to cloud, but seems like i'm missing something . The output is not as desired .
I expect the output to be having all images. But it is just 0 instead.
useEffect(() => {
console.log("============ From UseEffect ==========")
console.log("Images now Contain : ", images);
}, [images]);
const uploadFilesToCloud = async () => {
const data = new FormData();
for (let i = 0; i < imageUrls.length; i++) {
const finalImage = await getBase64FromUrl(imageUrls[i]);
data.append("file", finalImage);
data.append("upload_preset", "myPreset");
const res = await fetch(
"https://api.cloudinary.com/v1_1/dpurb6xes/image/upload",
{
method: "Post",
body: data,
}
);
const response = await res.json();
setImages((old) => [...old, response.secure_url]);
}
};
const onClick = async (e) => {
await uploadFilesToCloud();
const myForm = {
name: name,
imageUrls: images,
};
console.log("form data is ", myForm);
}
Suprisingly the output is :
My actual question is now .
- Though the images has one element before setting myForm.imgUrls = images. Even then myForm.imageUrls is having no element. How ?
- And what i want is , after both the received images are set only then the form should be initialized with images.
Can you please say how to do just that.