I have a problem where i use fetch two times in my function. If i execute the function without useNavigate both the fetch functions are executed properly, but with it only one of the fetch functions are executed. Im wondering if it has something to do with async
this is without using Navigate which works just fine
updateTrahcan = (messagedata) => {
fetch(
"url" + (this.state.MessageEdit.id) + ".json",
{
method: "PUT",
body: JSON.stringify(messagedata),
headers:{
"Content-Type": "application/json"
}
}
).then(() => {
fetch(
"url" + (this.state.MessageEdit.id) + ".json",
{
method: "DELETE",
body: JSON.stringify(messagedata),
headers:{
"Content-Type": "application/json"
}
}
)
})
}
and this is using Navigate where only one of the fetch functions are executed
updateTrahcan = (messagedata) => {
fetch(
"url" + (this.state.MessageEdit.id) + ".json",
{
method: "PUT",
body: JSON.stringify(messagedata),
headers:{
"Content-Type": "application/json"
}
}
).then(() => {
fetch(
"url" + (this.state.MessageEdit.id) + ".json",
{
method: "DELETE",
body: JSON.stringify(messagedata),
headers:{
"Content-Type": "application/json"
}
}
)
}).then(() =>{
this.props.navigation(0);
)
}
any help is appreciated
>Solution :
try to do this instead
updateTrahcan = async (messagedata) => {
await fetch(
"url" + (this.state.MessageEdit.id) + ".json",
{
method: "PUT",
body: JSON.stringify(messagedata),
headers:{
"Content-Type": "application/json"
}
}
)
await fetch(
"url" + (this.state.MessageEdit.id) + ".json",
{
method: "DELETE",
body: JSON.stringify(messagedata),
headers:{
"Content-Type": "application/json"
}
}
)
})
this.props.navigation(0);
}
hope this will do the job for you.