Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to navigate back to the page after two fetch functions are executed?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading