为什么axios总是陷入困境?

时间:2019-12-12 20:27:54

标签: react-native axios

即使状态码为200,axios也始终落在cath中,并且在没有错误的情况下返回:

  

[未处理的承诺拒绝:TypeError:未定义不是对象(正在评估'_context.t0.response.data')]

代码:

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

API AXIOS文件:

import axios from 'axios';

const api = axios.create({
    baseURL: 'http://192.168.1.45:8080'
});

export default api;

1 个答案:

答案 0 :(得分:0)

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