简体   繁体   English

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

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

I am creating a podcast app using react native and PHP/MySQL.我正在使用 react native 和 PHP/MySQL 创建一个播客应用程序。

I have stored each podcast and it's details in a MySQL database and am using PHP to get the information with a query.我已将每个播客及其详细信息存储在 MySQL 数据库中,并使用 PHP 通过查询获取信息。

I then fetch the information in react native and convert it to JSON format.然后我在 react native 中获取信息并将其转换为 JSON 格式。

I am using react-native sound in order to play the podcast within the app.我正在使用 react-native 声音来在应用程序中播放播客。

Here is my code.这是我的代码。

 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);
              }
          });    
      }
  }

I am trying to access the state of the Fileurl within the play function so that I can use it to access the correct file to play.我正在尝试访问播放函数中 Fileurl 的状态,以便我可以使用它来访问要播放的正确文件。

When I run my app I get the following error.当我运行我的应用程序时,出现以下错误。

TypeError: undefined is not an object (evaluating 'this.FileUrlHolder.bind')类型错误:未定义不是对象(评估“this.FileUrlHolder.bind”)

How do I fix this?我该如何解决?

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

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

暂无
暂无

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

相关问题 类型错误:未定义不是对象,本机反应 - TypeError: undefined is not an object, react native React Native TypeError:undefined 不是 object - React Native TypeError: undefined is not an object TypeError:未定义不是未定义的对象'createElement'-React Native - TypeError: undefined is not an object 'createElement' of undefined - React Native React-native-camera - TypeError: undefined is not an object - React-native-camera - TypeError: undefined is not an object TypeError: undefined is not an object in React-Native - TypeError: undefined is not an object in React-Native TypeError:未定义不是对象(评估style.width)是本机反应 - TypeError: undefined is not an object (evaluating style.width) react native React-Native:TypeError:undefined 不是对象(评估“_this.props.item”) - React-Native: TypeError: undefined is not an object (evaluating '_this.props.item') TypeError:undefined 不是 React Native 中的对象(正在评估“addTodo(title, description).then”) - TypeError: undefined is not an object (evaluating 'addTodo(title, description).then') in React Native TypeError:undefined 不是本机反应中的对象(正在评估“_reactNative.Stylesheet.create”) - TypeError: undefined is not an object (evaluating '_reactNative.Stylesheet.create') in react native React Native TypeError:undefined不是对象(评估'_ref.item') - React Native TypeError: undefined is not an object (evaluating '_ref.item')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM