繁体   English   中英

类型错误:未定义不是本机反应中的对象

[英]TypeError: undefined is not an object in react native

我正在使用 react native 和 PHP/MySQL 创建一个播客应用程序。

我已将每个播客及其详细信息存储在 MySQL 数据库中,并使用 PHP 通过查询获取信息。

然后我在 react native 中获取信息并将其转换为 JSON 格式。

我正在使用 react-native 声音来在应用程序中播放播客。

这是我的代码。

 constructor(props){
      super(props);
      this.FileUrlHolder = this.FileUrlHolder.bind(this);
      this.state = {
          playState:'paused', //playing, paused
          playSeconds:0,
          duration:0,
          FileUrlHolder: ''

      }

      this.sliderEditing = false;
  }

  componentDidMount(){
      fetch('http://da48c35f.ngrok.io/Filter.php', {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({

            // Getting the id.
            id: this.props.navigation.state.params.FlatListClickItemHolder
      })
    }).then((response) => response.json())
    .then((responseJson) => { 

        this.setState({ 
            FileUrlHolder: responseJson[0].FileUrl 
        }); 
        //console.log(responseJson[0].FileUrl);

    }).catch((error) => {
      console.error(error);
    })


  play = async () => {
      if(this.sound){
          this.sound.play(this.playComplete);
          this.setState({playState:'playing'});
      }else{

        const filepath = this.state.FileUrlHolder;
        console.log('[Play]', filepath);

          this.sound = new Sound(filepath, Sound.MAIN_BUNDLE, (error) => {
              if (error) {
                  console.log('failed to load the sound', error);
                  Alert.alert('Notice', 'audio file error. (Error code : 1)');
                  this.setState({playState:'paused'});
              }else{
                  this.setState({playState:'playing', duration:this.sound.getDuration()});
                  this.sound.play(this.playComplete);
              }
          });    
      }
  }

我正在尝试访问播放函数中 Fileurl 的状态,以便我可以使用它来访问要播放的正确文件。

当我运行我的应用程序时,出现以下错误。

类型错误:未定义不是对象(评估“this.FileUrlHolder.bind”)

我该如何解决?

FileUrlLoader 不是您的组件的属性,它是一个状态键,因此只需删除该绑定行

仅当您有名为 FileUrlHolder 的函数时才需要 this.FileUrlHolder = this.FileUrlHolder.bind(this)

暂无
暂无

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

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