2

I am calling displayElements from the render function and getting this error: 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(){
        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);
        });
      };

1 Answer 1

1

Write the conditon as if(data) and then write your code.

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

  }
Sign up to request clarification or add additional context in comments.

7 Comments

Now its saying this:TypeError: Cannot read property 'action' of undefined
how are you passing getObjectsQuery to this component ??
import getObjectsQuery from '../Queries';
then don't use props to access getObjectsQuery . console.log(getObjectsQuery) and check what it is logging
Upon initial loading this error shows up: TypeError: Cannot read property 'action' of undefined.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.