可能未处理的承诺拒绝(id 0)反应本机

时间:2020-05-04 17:01:27

标签: react-native axios fetch

此代码可在Expio.io联机上正常工作,但是当我尝试使用Visual Studio代码时出现网络错误 “可能的未处理的承诺拒绝(id 0)反应本机”

这是代码 '''

 state={
 Data:[]
 }   
 componentDidMount(){
 axios.get("http://localhost:63007/api/values/GetCoustomer")
.then(response => {
 this.setState({Data: response.data});
 console.log(response.statusText);
 })
.then(error => console.log(error));
 }

'''

1 个答案:

答案 0 :(得分:1)

您缺少捕获块。它应该看起来像这样:

axios.get("http://localhost:63007/api/values/GetCoustomer")
  .then(function (response) {
    // handle success
    this.setState({Data: response.data});
 console.log(response.statusText);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .then(function () {
    // always executed
  });