I am currently facing one issue as you see in the below code there are 4 img options but one image comes under the popup(img2) so all three images are saved successfully in the MongoDB except the img2(which comes from the popup)
<div className="mt-10 grid grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6">
<div className="sm:col-span-3">
<label
htmlFor="img1"
className="block text-sm font-medium leading-6 text-gray-900 font-bold"
>
Image1* (Thumbnail)
</label>
<div className="mt-2">
<div className="flex rounded-md shadow-sm ring-1 ring-inset ring-gray-300 focus-within:ring-2 focus-within:ring-inset focus-within:ring-orange-600 ">
<input
type="file"
{...register("img1", {
required: "name is required",
})}
id="img1"
className="block flex-1 border-0 bg-transparent p-2 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-sm sm:leading-6"
accept="image/*"
onChange={(e) => handleImageChange(e, 1)}
/>
</div>
</div>
</div>
<div className="relative">
<button
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
onClick={openPopup}
>
Open Popup
</button>
{isPopupOpen && (
<div className="fixed inset-0 z-50 flex items-center justify-center overflow-auto bg-gray-800 bg-opacity-75">
<div className="bg-white rounded-lg p-8">
<label
htmlFor="img2"
className="block text-sm font-medium leading-6 text-gray-900 font-bold"
>
Image2*
</label>
<h2 className="text-xl font-semibold mb-4">Upload Image</h2>
<input
type="file"
{...register('img2', {
required: 'name is required',
})}
id="img2"
className="block w-full border-gray-400 p-2 rounded"
accept="image/*"
onChange={(e) => handleImageChange(e, 2)}
// value={setImages}
/>
<button
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
onClick={closePopup}
>
Close
</button>
</div>
</div>
)}
</div>
{/* <div className="sm:col-span-3">
<label
htmlFor="img2"
className="block text-sm font-medium leading-6 text-gray-900 font-bold"
>
Image2*
</label>
<div className="mt-2">
<div className="flex rounded-md shadow-sm ring-1 ring-inset ring-gray-300 focus-within:ring-2 focus-within:ring-inset focus-within:ring-orange-600 ">
<input
type="file"
{...register('img2', {
required: 'name is required',
})}
id="img2"
className="block flex-1 border-0 bg-transparent p-2 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-sm sm:leading-6"
accept="image/*"
onChange={(e) => handleImageChange(e, 2)}
/>
// </div>
</div>
</div> */}
</div>
<div className="mt-10 grid grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6">
<div className="sm:col-span-3">
<label
htmlFor="img3"
className="block text-sm font-medium leading-6 text-gray-900 font-bold"
>
Image3*
</label>
<div className="mt-2">
<div className="flex rounded-md shadow-sm ring-1 ring-inset ring-gray-300 focus-within:ring-2 focus-within:ring-inset focus-within:ring-orange-600 ">
<input
type="file"
{...register("img3", {
required: "name is required",
})}
id="img3"
className="block flex-1 border-0 bg-transparent p-2 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-sm sm:leading-6"
accept="image/*"
onChange={(e) => handleImageChange(e, 3)}
/>
</div>
</div>
</div>
<div className="sm:col-span-3">
<label
htmlFor="img4"
className="block text-sm font-medium leading-6 text-gray-900 font-bold"
>
Image4*
</label>
<div className="mt-2">
<div className="flex rounded-md shadow-sm ring-1 ring-inset ring-gray-300 focus-within:ring-2 focus-within:ring-inset focus-within:ring-orange-600 ">
<input
type="file"
{...register("img4", {
required: "name is required",
})}
id="img4"
className="block flex-1 border-0 bg-transparent p-2 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-sm sm:leading-6"
accept="image/*"
onChange={(e) => handleImageChange(e, 4)}
/>
</div>
</div>
</div>
</div>
</div>
const handleImageChange = (e, index) => {
const file = e.target.files[0];
console.log(file);
if (file) {
const imageUrl = URL.createObjectURL(file);
console.log("imageUrl", imageUrl);
setImages(`img${index}`, imageUrl);
}
};
const openPopup = () => {
setIsPopupOpen(true);
};
const closePopup = () => {
setIsPopupOpen(false);
};
// Add images to FormData
for (let i = 1; i <= 4; i++) {
const fileInput = document.getElementById(`img${i}`);
const file = fileInput?.files[0];
if (file) {
formData.append(`image`, file);
}
}
payload after submit the from in this only 3 images is going to save but there is 4 images option
image: (binary)
title: mssanxasndj
patternNumber: asnjasndjsand
room: Living Room
designStyle: sasd
category: Flooring
demandtype: Best Seller
subCategory: Carpet Tiles
collection: asdasdsad
color: Oak Brown,Cherry Blossom,Teak
units: 10
unitType: sqft
totalPricePerUnit: 12
discountedprice: 8
perUnitType: sqft
specialprice:
perUnitPrice: 9
dimensions[length][value]: 12
dimensions[length][unit]: mm
dimensions[width][value]: 1
dimensions[width][unit]: mm
dimensions[thickness][value]: 1
dimensions[thickness][unit]: mm
image: (binary)
image: (binary)
image: (binary)
purchaseMode: Only Online,In-store request Only
otherRoom:
productDescription: asd
coreValues[0][heading]:
coreValues[0][text]:
features[0][feature]:
maintainanceDetails: asdasd
there is one add product add page in which i have to choose 4 images which going to save at MongoDB so as of now 3 images successfully saved at MongoDB but when i comes to img2 (popup) that image is not getting saved in mongo db
how can i resolve this issue ?