Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 可能的未处理承诺拒绝(id:0):未定义的不是对象(';prevComponentInstance.\u currentElement';)_Javascript_Json_Reactjs_React Native_React Native Android - Fatal编程技术网

Javascript 可能的未处理承诺拒绝(id:0):未定义的不是对象(';prevComponentInstance.\u currentElement';)

Javascript 可能的未处理承诺拒绝(id:0):未定义的不是对象(';prevComponentInstance.\u currentElement';),javascript,json,reactjs,react-native,react-native-android,Javascript,Json,Reactjs,React Native,React Native Android,在react native中,我尝试使用axios.get(my_url_path)获取json数据,然后使用response.data将我的状态更新为“urldatabase”键,如果我尝试调用此状态键并从获取的json读取数据,我会得到错误: Possible Unhandled Promise Rejection (id: 0): undefined is not an object ('prevComponentInstance._currentElement').... 我的代码如

在react native中,我尝试使用axios.get(my_url_path)获取json数据,然后使用response.data将我的状态更新为“urldatabase”键,如果我尝试调用此状态键并从获取的json读取数据,我会得到错误:

Possible Unhandled Promise Rejection (id: 0): undefined is not an object ('prevComponentInstance._currentElement').... 
我的代码如下:

  }
  constructor(props) {
    super(props);
    this.state = { userAnswer: '', count: 0, urldatabase: {} };
  }



  componentWillMount(){
    axios.get('my_url_path')
      .then(response => this.setState({urldatabase: response.data}));
  }
  render() {
    let num = this.state.count;
    console.log(this.state.urldatabase[num].book)
    const book = this.state.urldatabase[num].book
     return(
        <RiddleHeader headerText={book} />
)
}
}
}
建造师(道具){
超级(道具);
this.state={userAnswer:'',计数:0,urldatabase:{};
}
组件willmount(){
get('my\u url\u path')
.then(response=>this.setState({urldatabase:response.data}));
}
render(){
让num=this.state.count;
console.log(this.state.urldatabase[num].book)
const book=this.state.urldatabase[num].book
返回(
)
}
}

有人能解释一下我为什么会犯这个错误吗?如果我做错了什么?

考虑一下:在
组件willmount
中,如果axios承诺被拒绝会发生什么?右:它没有处理。你需要一个
catch
处理程序。承诺的基本规则:总是处理拒绝或将承诺退还给打电话的人。在这种情况下,由于如果您从
componentWillMount
返回承诺,React将不会对其进行任何处理,因此您需要处理
componentWillMount

中的错误,像这样重新布线代码,并检查是否再次发生

  }   constructor(props) {
    super(props);
    this.state = { userAnswer: '', count: 0, urldatabase: {} };   }



  componentWillMount(){
    axios.get('my_url_path')
      .then(response => this.setState({urldatabase: response.data}));   }   render() {
    let num = this.state.count;
    console.log(this.state.urldatabase[num].book)
    const book = this.state.urldatabase[num] && this.state.urldatabase[num].book; // this code can avoid undefined error
     return(
        <RiddleHeader headerText={book} /> ) } }
}构造函数(道具){
超级(道具);
this.state={userAnswer:'',计数:0,urldatabase:{};}
组件willmount(){
get('my\u url\u path')
.then(response=>this.setState({urldatabase:response.data}));}render(){
让num=this.state.count;
console.log(this.state.urldatabase[num].book)
const book=this.state.urldatabase[num]&&this.state.urldatabase[num].book;//此代码可以避免未定义的错误
返回(
) } }

您能否尝试绑定构造函数中装载的组件?另外,请添加带有适当缩进的整个文件。在构造函数中this.state={…}之后,添加以下内容:
javascript this.componentWillMount=this.componentWillMount.bind(this)