So I am trying to send an image to my node.js api, but logging the image hook returns undefined. I've tried multiple ways, but each time I get the same undefined messege, I've tried useState([]), useState("") and even the example shown below. Please help end my misery
import Cookies from "universal-cookie";
import FormData from "form-data";
export const CreateRecipe = () => {
const storageUrl = `http://${process.env.REACT_APP_API_URL}/storage/new`;
const cookies = new Cookies();
const token = cookies.get("token");
const [fileData, setFileData] = useState();
const getFile = (e) => {
setFileData(e.target.files[0]);
console.log(fileData);
};
const handleSubmit = async (e) => {
e.preventDefault();
const formData = new FormData();
formData.append("image", fileData);
fetch(storageUrl, {
method: "POST",
headers: {
Authorization: "Bearer " + token,
"Content-Type": "multipart/form-data",
},
body: formData,
})
.then((res) => res.json())
.then((data) => {
if (data.error) {
alert(data.message);
} else {
console.log(data);
}
});
};
return (
<div id="create-recipe">
<form onSubmit={handleSubmit}>
<input
type="file"
name="image"
accept="image/*"
//style={{ display: 'none' }}
onChange={getFile}
/>
<button type="submit">Post </button>
</form>
</div>
);
};