I have create a function that uploads a image wist axios allong with text to my API
everything works fine. but after i have uploaded it i direclty I want to call the “getData()” function to direclty show the new created data in a table. but its not working direclty after
Upload code:
onFileUpload = () => {
const formdata = new FormData();
formdata.append(
"productFile.File",
this.state.FileSelect,
this.state.FileSelect.name
);
formdata.append("productFile.Title", this.state.Title);
formdata.append("productFile.Description", this.state.Description);
formdata.append("productFile.Price", this.state.Price);
formdata.append("productFile.Quantity", this.state.quantity);
console.log(this.state.FileSelect);
axios.post("api/Upload", formdata);
this.getData();
the getData() function:
async getData() {
const url = 'api/Upload/';
const response = await fetch(url);
const data = await response.json();
console.log(data);
this.setState({
allproducts: data,
loading: false
});
}
when i refresh the page it works fine and i see the new data because i use “componentDidMount()” with the getData function:
componentDidMount() {
this.getData();
}
why does it not work on the "onFileUpload " as well
is it because the data has not been uploaded yet when i call the “getData”
function. How do i solve this?