TypeError:未定义不是对象(正在评估'_ this.props.navigation.state.params.datas.map)

时间:2019-03-04 13:15:54

标签: react-native

我将数据发送到Page1组件。然后我从那里到达

  

this.props.navigation.state.params.datas.map

当应用程序处于调试模式时,一切正常。但是当我停止调试时,它给了我那个错误的提示

我这样发送:

 controlAllMeth() {
      Promise.all([this.callFetch(), this.startAnim()])
        .then(([fetchResponse, animResponse]) => {
          this.setState({PersonalInfo: fetchResponse[0]});

          if(this.state.username != '')
          {
            this.props.navigation.navigate('Page1',{datas: this.state.PersonalInfo});
          }
          else
          {
            this.props.navigation.navigate('login');
          }
        })
        .catch(err => {

        });
    }

我这样看:

    constructor(props) {
            super(props);
this.props.navigation.state.params.datas.map(color => {
            if(color.Renk == 'MAVI'){
                Blue.push("MAVI");
            }
            else if(color.Renk == 'SARI')
            {
                Yellow.push("SARI")
            }
        })
      }

我在Promise.all中使用controlAllMeth()等待网络服务完成。在调试模式下可以,但是如果启用调试,则可以正常工作。

我在哪里称呼网络服务

callFetch() {
      let collection={}
      collection.username = this.state.username,
      collection.password = this.state.password
      var x = [] ;
      fetch('url', {
            method: 'POST',
            headers: new Headers({
                          Accept: 'application/json',
                        'Content-Type': 'application/json', // <-- Specifying the Content-Type
                }),
            body: JSON.stringify(collection) // <-- Post parameters
            })
            .then((response) => response.text())
            .then(leaders => {
                const str = leaders.substring(6);
                const str4 = str.substring(0,str.length - 2);
                const str5 = str4.replace(/\\/g, '');
                const str3 = JSON.parse(str5);

                console.log("callfetch",  str3);

                        //  this.savedata(collection.username,collection.password)
                x.push(str3);

            });
            return x;
    }

enter image description here

1 个答案:

答案 0 :(得分:1)

当您尝试访问controllAllMeth之前,您的问题出在state函数中,因此该值将是undefined

setState中使用回调,或者在使用值之前定义值。最简单的方法是定义值:

定义值

controlAllMeth() {
  Promise.all([this.callFetch(), this.startAnim()])
    .then(([fetchResponse, animResponse]) => {
      const datas = fetchResponse[0] // define the value
      this.setState({PersonalInfo: datas});

      if(this.state.username != '')
      {
        this.props.navigation.navigate('Page1',{datas: datas}); // use the value you defined rather than the value from state
      }
      else
      {
        this.props.navigation.navigate('login');
      }
    })
    .catch(err => {

    });
}

使用回调

controlAllMeth () {
  Promise.all([this.callFetch(), this.startAnim()])
    .then(([fetchResponse, animResponse]) => {
      this.setState({ PersonalInfo: fetchResponse[0] }, () => { // use a callback to guarantee state has been updated
        if (this.state.username != '') 
        {
          this.props.navigation.navigate('Page1', { datas: this.state.PersonalInfo });
        } 
        else 
        {
          this.props.navigation.navigate('login');
        }
      });
    })
    .catch(err => {

    });
}

关于setState

的一些好文章