Home > Back-end >  Not Getting anything back after calling Fetch API - React Native
Not Getting anything back after calling Fetch API - React Native

Time:12-16

I am creating a React Native in which i am sending my Form's data to Backend Node.js using Fetch and that worked all fine but i cannot execute anything down after fetch api, even console.log is not running.

React-Native Code:

const PostData  = () =>{

        console.log("Posting");


        //Sending Request to Node.js using Fetch API
        fetch("http://192.168.0.107:3000/Adminsignup", {

            //Setting Method
            method:"POST",

            //Setting Headers
            headers:{
                //Setting Content-Type
                "Content-Type" : "application/json"
            },
            //Stringifying the email and password and storing it into body
            body:JSON.stringify({
                name,
                gmail,
                password,
                retype
            })
        }).then(res=>{
            console.log(res);
        }).catch(err=>{
            console.log(err);
        })
    } 

.then and .catch of fetch api is not working.

CodePudding user response:

Ok so your front-end code is all good and as u said that your backend is also working when you fire PostData() function, check if you are returning the response from backend.

Add this in your signup Route:

res.status(200).send({result:"Successfully got Response"})

Catch status in your front-end like this:

let final = await fetch("http://192.168.0.107:5000/studentSignup", {
            //Setting Method
            method:"POST",
            //Setting Headers
            headers:{

                //Setting Content-Type
                "Content-Type" : "application/json"
            },
            //Stringifying the email and password and storing it into body
            body:JSON.stringify({name,gmail,password,retype})
        })

        const data = final.status;
        if(data === 200)
        {
            navigation.navigate("Your Route");
        }
  • Related