捕获块:无法读取未定义的 属性 'catch'
Catch block: can not read property 'catch' of undefined
我的 catch 块有问题,它不工作并抛出错误。
当我在添加该块之前添加 NetInfo
if-else 块时出现此错误,但它在添加该块后显示该错误。
这是我在 login.js
中的代码
componentDidMount() {
const { navigate } = this.props.navigation;
NetInfo.isConnected.fetch().done((isConnected) => {
if ( isConnected )
{
return fetch('http://10.42.0.1:8000/stocks/',
{
method: "GET",
headers: {
'Authorization': `JWT ${"eyJhbGciOiJIUz1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InNhbSIsInVzZXJfaWQiOjYxLCJlbWFpbCI6IiIsImV4cCI6MTUwNDAzNDUzOX0.d3tbOUS_0L9RzkWA30DT8mv7v7j0XuxnRuI_luhuNzI"}`
}
})
.then((response) => response.json())
.then((responseJson) => {
if (!responseJson.post){
navigate("Login");
}else
{
let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.setState({
isLoading: false,
sa: ds.cloneWithRows(responseJson.post),
});
}
})
}else
{
navigate("Login");
Alert.alert(
'Check Internet connection',
)
}
}).catch((error) => {
console.log(error);
})
}
我哪里错了?请帮忙...
尝试将您的 .done 更改为 .then。
我还建议将其中的一些逻辑提取到单独的函数中,以便于您自己阅读。到处都是大括号和圆括号!
我的 catch 块有问题,它不工作并抛出错误。
当我在添加该块之前添加 NetInfo
if-else 块时出现此错误,但它在添加该块后显示该错误。
这是我在 login.js
componentDidMount() {
const { navigate } = this.props.navigation;
NetInfo.isConnected.fetch().done((isConnected) => {
if ( isConnected )
{
return fetch('http://10.42.0.1:8000/stocks/',
{
method: "GET",
headers: {
'Authorization': `JWT ${"eyJhbGciOiJIUz1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InNhbSIsInVzZXJfaWQiOjYxLCJlbWFpbCI6IiIsImV4cCI6MTUwNDAzNDUzOX0.d3tbOUS_0L9RzkWA30DT8mv7v7j0XuxnRuI_luhuNzI"}`
}
})
.then((response) => response.json())
.then((responseJson) => {
if (!responseJson.post){
navigate("Login");
}else
{
let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.setState({
isLoading: false,
sa: ds.cloneWithRows(responseJson.post),
});
}
})
}else
{
navigate("Login");
Alert.alert(
'Check Internet connection',
)
}
}).catch((error) => {
console.log(error);
})
}
我哪里错了?请帮忙...
尝试将您的 .done 更改为 .then。
我还建议将其中的一些逻辑提取到单独的函数中,以便于您自己阅读。到处都是大括号和圆括号!