react-native - 未处理的承诺拒绝:TypeError:网络请求在博览会中失败
问题描述
我正在尝试使用我的 api,这是代码
在这里,我创建了家庭课程和状态
export default class Home extends React.Component {
state = {
region: null,
places: []
}
在这里我连接到我的 api 并尝试搜索这些地方
async componentDidMount() {
fetch('http://localhost:3000/auth/list')
.then(res => res.json())
.then(res =>{
this.setState({
places: res
})
})
在这里,我将它呈现在地图中
render() {
const {region} = this.state;
return (
<View style={styles.Container}>
<MapView style={styles.Mapview}
region={region}
showsUserLocation={true}
zoomEnabled={true}
showsPointsOfInterest={false}
initialRegion={region}
>
在这里我创建一个标记
{this.state.places.map(places => (
<MapView.Marker
key={places.id}
title={places.title}
description={places.description}
coordinate={{
latitude: places.latitude,
longitude: places.longitude
}}
/>
))}
</MapView>
但它说:未处理的承诺拒绝:TypeError:网络请求失败
解决方案
这将抛出一个应该使用 catch 函数捕获的错误。试试这个方法
return fetch(url)
.then(function (response) {
return response.json();
})
.then(function (res) {
this.setState({
places: res,
});
})
.catch(function (error) {
console.log("Error");
// ADD THIS THROW error
throw error;
});
推荐阅读
- .net-core - IBM DB2 连接器核心的 iSeries 连接错误
- flutter - 如何在 Flutter 中创建一个容器而不给它一个高度
- powershell - 使用 TF.exe 时 Azure 管道“忘记”身份验证
- cmake - 即使 Freetype 包含在 cmake 中,也找不到 Freetype 标头
- javascript - 使用 javascript 从 chrome 控制台抓取网站
- r - 连接箱线图意味着在R中的折线图上
- c# - 如何在 VSTO 中添加现有 Excel 加载项
- jquery - JSON查询表
- javascript - 如何使用html2pdf向pdf添加水平线
- python - 如果具有特定数字的除法余数为零,则打印 - Python