When I send data to the API, I get an error image : [‘The image must be an image.’]. How can I send it?
function saveFile() {
while (imagePreview.firstChild) {
imagePreview.removeChild(imagePreview.firstChild);
}
const files = fileInput.files;
for (let i = 0; i < files.length; i++) {
const file = files[i];
let data = JSON.parse(localStorage.getItem("data")) || {};
data.image = [];
if (file.type.startsWith("image/")) {
const img = document.createElement("img");
img.classList.add("resume--photo");
const reader = new FileReader();
reader.onload = () => {
img.src = reader.result;
const fileInfo = {
name: file.name,
type: file.type,
dataURL: reader.result,
};
data.image.push(fileInfo);
localStorage.setItem("data", JSON.stringify(data));
};
reader.readAsDataURL(file);
imagePreview.appendChild(img);
}
}
}
The rest of the data information is sent, only the image is not sent
const formEl = document.querySelector(".form");
formEl.addEventListener("submit", function (e) {
e.preventDefault();
const data = generateFormData();
fetch("https://resume.redberryinternship.ge/api/cvs", {
method: "POST",
headers: {
Accept: "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST,PATCH,OPTIONS",
},
body: JSON.stringify(data),
})
.then((res) => res.json())
.then((data) => console.log(data))
.catch((error) => console.log(error));
console.log(data);
});
This generates data which I then send
function generateFormData() {
const form = document.getElementById("form");
const formData = new FormData(form);
const data = {
experiences: [],
educations: [],
};
for (let [key, value] of formData.entries()) {
data[key] = value;
}
const storedData = JSON.parse(localStorage.getItem("data"));
if (storedData) {
Object.assign(data, storedData);
}
return data;
}
I have tried different ways but still can’t solve the problem