“ SyntaxError:JSON输入意外结束”

时间:2018-12-20 14:22:42

标签: javascript react-native

尝试使用async / await从API获取数据,但是在会话期满后面临上述错误,如何处理该错误并显示用户会话已过期或其他任何有用的错误?

export const walletAddCardRequest = async (dispatch, token, payload, url) => {
  try {

      persist.store.dispatch(setLoader(true));
      const headers = {Authorization: token}
      const response = await api(url, "POST", payload, headers);
      console.log("wallet add card ", response);
      persist.store.dispatch(setLoader(false));
      if (response.status === 200) {
          const user = await response.json();
          if (user) {
              persist.store.dispatch(setLoader(false));
              Alert.alert(
                  "",
                  "Your card added successfully."
              );
                navigateTo("wallet");
          } else {
              throw new Error("Error. Please try again");
          }
      } else {}
  } catch (e) {
      persist.store.dispatch(setLoader(false));
      Alert.alert(e.message);
      console.log("console:",e);
      dispatch({
          type: AUTH_ERROR
      });
  }
}

0 个答案:

没有答案