我有一个应用程序,其中定义了所有父帖子并获取了调用。我正在尝试实现一种逻辑,如果有任何API向我发送401状态错误,则必须路由到首页。我尝试了该文件中的各种方法,但是我正在寻找的每个解决方案都无法正常工作。 以下是我的Server.js代码:
import React, { Component } from "react";
import { Alert, AsyncStorage} from 'react-native';
import { RNS3} from "react-native-aws3";
import { NavigationActions, navigate, navigation } from "react-navigation";
export default class Server extends Component {
constructor(props) {
super(props);
participantId = "";
mobeToken = "";
mobeToken1 = "";
participantId1 = "";
Url = "";
urlAsync = "";
AsyncStorage.getItem("participantId", (err, participantId) => {
participantId1 = participantId;
});
AsyncStorage.getItem("mobeToken", (err, mobeToken) => {
mobeToken1 = mobeToken;
});
AsyncStorage.getItem("URL", (err, Url) => {
urlAsync = Url;
});
this.assignValues(participantId, mobeToken, Url);
}
assignValues(participantId, mobeToken, Url) {
participantId1 = participantId;
mobeToken1 = mobeToken;
urlAsync = Url;
}
handleLog(){
// const { navigate } = this.props.navigation;
// navigation.navigate("PersonProfile");
alert("hi");
// AsyncStorage.setItem("session", "no", () => {
// this.props.navigation.dispatch(
// NavigationActions.reset({
// index: 0,
// actions: [NavigationActions.navigate({ routeName: "Home" })]
// })
// );
// });
};
getRequest(url) {
return fetch(urlAsync+"/api" + url , {
method: 'GET',
headers: {
"mobeToken" : mobeToken1,
"participantId": participantId1,
}
})
.then(response => response.json())
.then(responseJson => {
if (responseJson.error === 'authentication.error' ){
this.handleLog();
}
return responseJson;
})
.catch(error => {
console.log(error);
// Alert.alert("", "Network connection issue. Please contact support");
alert(error);
});
}
}
在此情况下,如果响应中出现错误,则必须在尝试实现导航逻辑的地方调用handleLog()方法。 让我知道该如何解决/或对此进行任何修复。