Axios falls in catch even with status code 200

Asked

Viewed 417 times

2

I have a login code, that when a user logs in if the data is incorrect a backend done in nodejs returns an error 401 and if everything is correct returns a status code of 200.

During the execution of the code, when the user is wrong in the data he performs the function correctly "catch", but if he puts everything correct he does not know by soul who executes the then but also the "catch".

Someone could help me with this problem?

        async function handleLogin(){
        await AsyncStorage.removeItem('Authorization');
        await AsyncStorage.removeItem('ImAuthenticated');
        await api.post('/login', {
            email,
            password
        }, {
            headers: { 'device': 'mobile' }
        }).then(async(response)=>{
            const { hash } = response.data;
            await AsyncStorage.setItem("Authorization", hash);
            await AsyncStorage.setItem("ImAuthenticated", true);
            navigation.navigate('Menu');
            console.log('oi');
        }).catch(function (error){
            if(error.response.data.showIn == "text"){
                setShowInfo(true);
                setEmail('');
                setPass('');
                this.InEmail.focus();
                if(error.response.data.level == 3){
                    setColorInfo(false);
                }else{
                    setColorInfo(true);
                }
                setInfoText(error.response.data.error);
            }else{
                setshowBox(true);
                setEmail('');
                setPass('');
                this.InEmail.focus();
                if(error.response.data.level == 3){
                    setcolorBox(false);
                }else{
                    setcolorBox(true);
                }
                setboxText(error.response.data.error);
        }
        });
    }

2 answers

1

Oops, I imagine that if your request returns 200, the problem is not in it but in some logic within the answer to your request...

can be Asyncstorage, or navigation. They are imported correctly ?

IMPORTANT: so you know what’s wrong with the Answer of your request, put a console.log in the catch. .catch(function (error){ console.log('erro', error ); } Then you look at the console and you will surely have clarity of what went wrong.

Comment here on the problem that the catch pointed out if it can not solve.

0

If you are using the async\await it is not necessary to use the resolve\reject. Try modifying your code to:

// ...
try {
    const dados = await api.post('/login',
                                 { email, password },
                                 { headers: { 'device': 'mobile' } }
    );

    // ...
    console.log(dados);
    // ...
} catch (err) {
    console.log('Erro no api.post', err);
}

or without the async/await:

api.post('/login',
         { email, password },
         { headers: { 'device': 'mobile' } }
).then(dados => {
    // ...
    console.log(dados);
    // ...
}).catch(err => {
    console.log(err);
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.