【问题标题】:How to catch error when Backendless return empty array to axios - react native当Backendless将空数组返回到axios时如何捕获错误 - 反应原生
【发布时间】:2021-08-21 11:05:21
【问题描述】:

我有一个 React-Native 代码,可以从 backEndless 检索 REST API 的数据。

在某些情况下,数据是一个空数组。我的代码返回此错误:

error [TypeError: undefined is not an object (evaluating 'res.data[0]['ownerId']')]

这意味着 AXIOS 输入 .then 部分而不是输入 .catch 部分,它接受空数组作为结果。

我应该得到的是 Request Failed with code 401,但我无法捕捉到错误。

有什么帮助吗?

这是我的代码:

export const confirmCode = (phone,code)=>{
    return (dispatch,getState)=>{
        dispatch(confirmCodeStart());
        phone = phone.substring(2);
        console.log(''+code+':'+phone+'');


        axios.get('/data/Auth?where=code='+code+'&phone='+phone+'')
           .then((res) =>{
               
               console.log(res.status);
               console.log('entereeeeed');
                ownerid = res.data[0]['ownerId'];
            
            
               
               
               //console.log(res.data[0]['ownerId']+"aaaaa");
               //getUser(res.data[0]['ownerId'],phone,props);
               ////////
               
                    //api request
                    axios.post('/users/login',{'objectId':ownerid})
                    .then(res2=>{
                        dispatch(confirmCodeSuccess());
                        //console.log(res.data);
                        userToken=res2.data['user-token'];
                        userData=res2.data;
                        console.log(res2.data);
                        //const {token,user} = res2.data;????????
                        axios.defaults.headers.Authorization ='Bearer '+userToken;
                        //dispatcher.dispatch({type:'SET_TOKEN',payload:{token:userToken}});
                        //dispatcher.dispatch({type:'SET_USER',payload:{user:userData}});
                        dispatch(setToken(userToken));
                        dispatch(setUser(userData));
                        AsyncStorage.setItem(TOKEN_KEY,userToken);
                        AsyncStorage.setItem(USER_KEY,JSON.stringify(userData));
                        
                        
                    })
           })
           .catch(error=>{
            Reactotron.log('error',error);
               console.log('error',error);
               dispatch(confirmCodeFailure());
               
           })     
    };
};

【问题讨论】:

    标签: android ios react-native axios backendless


    【解决方案1】:

    你应该抛出移动编译器来捕捉部分

    ...
    
            axios.get('/data/Auth?where=code='+code+'&phone='+phone+'')
               .then((res) =>{
                   if(!res.data) {
                      throw res
                   }
                   
                   console.log(res.status);
                   console.log('entereeeeed');
                    ownerid = res.data[0]['ownerId'];
    ...
    

    当你 throw 进入 then 部分时,编译器会转到最近的 catch 部分

    我给我的朋友做下面的例子来了解 promise 的工作流程

    new Promise(function(resolve, reject) {
      setTimeout(() => reject(true), 1000);
    }).then(() => {
    console.log("heh1")
    throw true
    }).catch(() => {
    console.log("heh2")
    }).then(() => {
    console.log("heh3")
    throw true
    }).then(() => {
    console.log("heh4")
    }).then(() => {
    console.log("heh5")
    }).catch(() => {
    console.log("heh6")
    })
    

    结果:

    heh2
    heh3
    heh6
    

    【讨论】:

    • 感谢您的回答。我仍然得到同样的错误。它不是为了捕捉而扔的
    • 条件不存在,我尝试控制台记录 IF 条件,但没有打印任何内容
    • 我试过这个并且成功了:if(!res.data || res.data[0]===undefined) 问题是,我可以抛出 res 并显示错误消息吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 2021-10-20
    相关资源
    最近更新 更多