0
votes

I am trying to make a post request using axios in my react native code.

My backend is in Node.js and I am using Postman to test it out. Here, you can see that my endpoint aip/user/login sends back a token and id for the user with the email and password specified in the body.

enter image description here

My React Native code is the following :

export default function Signin(props) {



    const [email, setEmail] = useState(''); 
    const [password, setPassword] = useState('');
    const [isPasswordVisible, setIsPasswordVisible] = useState(false)

    const login = () => {
        // on teste le format de l'email
        let isEmailValid = emailRegex.test(email); 
        if (isEmailValid) {
            const user = {
                email: email,
                password: email
            }


       axios.post(`http://localhost:3002/api/user/login`, { user })
            .then(response => {
                console.log(response)
                console.log(response.data)
            })
            .catch(error => console.log(error))
        else {
            alert("Email invalid")
        }
    }


    return (

        <View style={styles.container}>
                    <TextInput
                        label="Email"
                        value={email}
                        onChangeText={email => setEmail(email)}
                    />
                    <View style={styles.password}>
                        <TextInput
                            label="Password"
                            value={password}
                            onChangeText={password => setPassword(password)}
                        />
                    </View>

                    <Button mode="contained" onPress={login} style={styles.signin}> Signin</Button>
        </View>
    )
}

And this is the error I get. I don't even see the console.log(response) and the console.log(response.data) results. What am I doing wrong?

![enter image description here

4
The promise that Axios returns has been rejected. So your error will be in a catch() handler on the promise. The error indicates that you've not created any rejection handling. - Emiel Zuurbier
Edited my code with what I tried. I added catch() block but still same problem - colla

4 Answers

1
votes

Javascript promises can either be resolved (i.e. the requested resource was successfully retrieved) and rejected(i.e. the requested resource could not be retrieved).

The then() function only executes if the promise is resolved. Under the hood, if axios receives a response with an error code(Anything other than 2XX), then the promise will be rejected. Axios

In the case that a promise is rejected, you need to use the catch() callback. If you want to print the error, it should look something like the following:

axios.post(`http://localhost:3002/api/user/login`, { user })
                .then(response => {
                    console.log(response)
                    console.log(response.data)
                })
                .catch(error => console.log(error))

Essentially, .then() is not being executed because the request was unsuccessful, and a response other than 200/201/2XX was returned.

In your case, the issue is that you are passing {user}, where user is an object, so you are nesting the object. Remove the curly braces around user.

axios.post(`http://localhost:3002/api/user/login`, user)

You can read more about javascript promises, .then(), .catch() and .finally() here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

0
votes

Solution for me was instead of using localhost:port for the API address, actually using an IP address of the server.

Example:

axios.post(`http://10.0.0.2:3002/api/user/login`, { user })
     .then(response => {
        console.log(response)
        console.log(response.data)
})

Take a look here too: https://github.com/facebook/react-native/issues/10404

0
votes
  • first

check if the request is passed to server by : log a message whene the login function is called. console.log("called") if it called from the react native app (front) if not called i think that you have to link the server side with the client side


the error "network error " refers to that problem

0
votes

I had the same problem. Solution is the following:

  • If you working on your smartphone , make sure to have your computer and device connected to the same Network ( Wi-fi).
  • Don't use localhost , go to cmd and type ipconfig and copy the IPv4 Adress from Wireless LAN adapter Wi-Fi and make a call like that: " axios.post(http://192.168.0.111:3002/api/user/login, { user }) "
  • You need to open the port to allow access through Windows Firewall (if you working on windows)
  • If you don't know how open a port on windows firewall , click on the link or paste it on google (https://www.youtube.com/watch?v=xMGPyZtdP00) and you will see a detailed tutorial.

I would like to make a suggestion, in the future try to use the async / await syntax (this is what I use) instead of .then (), it makes your work easier and easy to understand.