我在React Native中使用此简单的api调用遇到问题,我也不知道为什么。我在reactJs上做了完全相同的事情,并且可以完美地在浏览器中工作。
const FlatListGames =({导航})=> {
const [games,setGames] = useState([]);
useEffect(() => {
function fetchAPI(){
axios.get('http://127.0.0.1:5000')
.then(response => {
setGames(response.data)
})
.then(error => console.log(error));
}
fetchAPI();
}, [])
console.log(games);
[未处理的承诺被拒绝:错误:网络错误]
答案 0 :(得分:-1)
之所以发生这种情况,是因为您没有使用.catch
,而是使用了.then
。
.then
用于成功撤销承诺时,.catch
用于发生错误时,因此您需要用then
替换第二个catch
。>
const FlatListGames = ({ navigation }) => {
const [games,setGames] = useState([]);
useEffect(() => {
function fetchAPI(){
axios.get('http://127.0.0.1:5000')
.then(response => {
setGames(response.data)
})
.catch(error => console.log(error));
}
fetchAPI();
}, [])
console.log(games