【问题标题】:In my React Native app, the catch block always executes even though the request is successful在我的 React Native 应用程序中,即使请求成功,catch 块也会始终执行
【发布时间】:2017-03-13 04:02:28
【问题描述】:

在我的 React Native 应用程序中,我只是登录用户或创建帐户,但即使用户成功登录或成功添加到数据库,我的登录和注册功能的 catch 块也会始终执行。所以这意味着success块(then)和catch块都是背靠背执行的。

除了 catch 块错误之外,我还看到一些黄色框警告说我的 JS 相对于 TextInput 组件太慢了。我不知道这些是否与 catch 块问题有关。

这个错误也影响了其他人,但我还没有听说有人找到解决方案。我已经在邮递员中测试了我的登录和注册路线,它们按预期工作,没有问题。

这里是登录和注册操作创建者:

登录:

export const loginUser = ({ email, password }) => {
   const props = { email, password };
   return (dispatch) => {
     // notifies REDUX store that an ajax request is in progress
     dispatch({ type: types.LOGIN_USER_START });

     axios.post('http://localhost:3000/signin', props)
       .then(user => loginUserSuccess(dispatch, user))
       .catch(() => loginUserFail(dispatch));
   };
 };

注册:

export const signupUser = ({ email, password }) => {
  const props = { email, password };
  return (dispatch) => {
    // notifies REDUX store that an ajax request is in progress
    dispatch({ type: types.SIGNUP_USER_START });

    axios.post('http://localhost:3000/signup', props)
      .then(user => signupUserSuccess(dispatch, user))
      .catch(() => {
        signupUserFailed(dispatch);
      });
  };
};

【问题讨论】:

  • 嘿,你能发布完整的错误日志吗?

标签: react-native ios-simulator redux


【解决方案1】:

您的错误发生在loginUserSuccess 的某处。而不是catch(()=>,您应该使用catch((err)=> 并对错误进行处理,例如console.log(err.stack),然后您就会确切地知道

  • 怎么了
  • 问题出在哪里

【讨论】:

    猜你喜欢
    • 2019-04-22
    • 2019-05-29
    • 2016-08-23
    • 2019-03-04
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    相关资源
    最近更新 更多