简体   繁体   English

ReactJS/Javascript/Graphql/React-apollo:无法读取未定义的属性“加载”

[英]ReactJS/Javascript/Graphql/React-apollo: Cannot read property 'loading' of undefined

I am calling displayElements from the render function and getting this error: TypeError: Cannot read property 'loading' of undefined .我正在从渲染函数调用 displayElements 并收到此错误: TypeError: Cannot read property 'loading' of undefined Why is this error showing up?为什么会出现这个错误? The reason I think this error is showing up is because here will be no results upon initial rendering.我认为出现此错误的原因是因为初始渲染时没有结果。 But when the submit button is click a query to the DB gets executed and I would like to display the results using the displayElements.但是,当单击提交按钮时,会执行对数据库的查询,我想使用 displayElements 显示结果。

displayElements(){
        var data = this.props.getObjectsQuery;
        console.log(this.props);
        if(data.loading){
          return <option disabled>Loading...</option>;
        }else{
          return data.action.map(action => {
            return(<option key={action.action} value={action.timestamp}>{action.filename}</option>);

          })
        }
      }

Submit Button提交按钮

handleSubmit = event => {
        event.preventDefault();

        console.log(this.state);
        this.setState({
          startTime: new Date(document.getElementById("startTime").value).valueOf(),//getElementById is a jQuery method
          endTime: new Date(document.getElementById("endTime").value).valueOf()
        }, () => {
          this.props.data.refetch({//Assign the inputvalues, which is the current state, to the variables after pressing the submit button
            startTime: this.state.startTime,
            endTime:this.state.endTime
          });
          console.log(this.state.startTime);
          console.log(this.state.endTime);
        });
      };

Write the conditon as if(data) and then write your code.if(data)一样编写条件,然后编写代码。

  displayElements(){
    var data = this.props.getObjectsQuery;
    console.log(this.props);
    if(data) {
      if(data.loading){
        return <option disabled>Loading...</option>;
      }else{
        return data.action.map(action => {
          return(<option key={action.action} value={action.timestamp}>{action.filename}</option>);
        })
      }
    }

  }

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

相关问题 ReactJS/Javascript/Graphql/React-apollo:无法读取未定义的属性“名称” - ReactJS/Javascript/Graphql/React-apollo: Cannot read property 'name' of undefined 无法读取未定义的属性“地图”(ReactJS-Apollo-Graphql) - Cannot read property 'map' of undefined (ReactJS-Apollo-Graphql) Graphql react-apollo内省片段匹配器 - Graphql react-apollo IntrospectionFragmentMatcher Apollo GraphQL - 类型错误:无法读取未定义的属性“findOrCreateUser” - Apollo GraphQL - TypeError: Cannot read property 'findOrCreateUser' of undefined javascript无法读取graphql查询中未定义的属性“0” - javascript cannot read property '0' of undefined in graphql query Graphql react-apollo IntrospectionFragmentMatcher问题 - Graphql react-apollo IntrospectionFragmentMatcher issues 如何使用React-Apollo处理GraphQL错误? - How to handle GraphQL errors with React-Apollo? ReactJS-TypeError:无法读取未定义的属性“ setState”-LocalForage IndexedDB GraphQL - ReactJS - TypeError: Cannot read property 'setState' of undefined - LocalForage IndexedDB GraphQL Graphql,react-apollo如何在加载组件状态时将变量传递给查询 - Graphql, react-apollo how to transfer variable to query at loading component state React Apollo:未捕获(承诺)类型错误:无法读取未定义的属性“重新获取” - React Apollo: Uncaught (in promise) TypeError: Cannot read property 'refetch' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM