简体   繁体   English

React Native:undefined不是一个对象(评估'_this.setState')

[英]React Native: undefined is not an object (evaluating '_this.setState')

I'm at a bit of a loss why I'm getting this error message in the simulator with the code below. 我有点茫然,为什么要在模拟器中使用以下代码获取此错误消息。 The line # is indicated in a comment. 行号在注释中指示。

this.setState seems to be in the correct object format. this.setState似乎是正确的对象格式。

  fetchData: function() {
    fetch(apiUrl)
      .then((response) => response.json())
      .then((responseData) => {
        this.collectResponses(responseData);
      })
      .then((responseObject) => {
        this.sortPredictions(responseObject);
      })
      .then((predictionsObj) => {
        this.setState({   // ERROR HERE!
          castroInKTLM: predictionsObj['5728'],
          churchInKTLM: predictionsObj['5726'],
          churchInJ: predictionsObj['4006'],
          churchInN: predictionsObj['4448'],
          montgOut: predictionsObj['6994'],
          church37In: predictionsObj['7073'],
          church37Out: predictionsObj['5667'],
          castro37Out: predictionsObj['3325'],
          castro33In: predictionsObj['3255'],
          fetchTime: predictionsObj['fetchTime']
        });
      })
      .done();
  },

I have not done react-native yet, but i believe the error you are seeing, is due to this not within the function scope (the one that receives predictionsObj) as parameter 我还没有做本机反应,但是我相信您看到的错误是由于此原因不在函数范围(接收预言的对象)作为参数的情况下

fetchData: function() {
    var self = this;
    fetch(apiUrl)
      .then((response) => response.json())
      .then((responseData) => {
        this.collectResponses(responseData);
      })
      .then((responseObject) => {
        this.sortPredictions(responseObject);
      })
      .then((predictionsObj) => {
          self.setState({   
          castroInKTLM: predictionsObj['5728'],
          churchInKTLM: predictionsObj['5726'],
          churchInJ: predictionsObj['4006'],
          churchInN: predictionsObj['4448'],
          montgOut: predictionsObj['6994'],
          church37In: predictionsObj['7073'],
          church37Out: predictionsObj['5667'],
          castro37Out: predictionsObj['3325'],
          castro33In: predictionsObj['3255'],
          fetchTime: predictionsObj['fetchTime']
        });
      })
      .done();
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM