0
componentDidMount(){
  axios.get('/sites/multiscreen/templates').then(res => {
    if(res.data.template_id.match(/^[a-z0-9]+$/i)){
      this.setState({
        templates: res.data,
      });
    }
  })
}

The error I'm getting is this:

Unhandled Rejection (TypeError): Cannot read property 'match' of undefined

Any help is appreciated!

3
  • 1
    Sounds like template_id isn't a string. Commented Mar 18, 2020 at 2:49
  • It's JSON... how would I use match() with json? Convert it to a string? Commented Mar 18, 2020 at 2:56
  • 1
    Can you post up your res.data? Error is saying that res.data.template_id is undefined. Commented Mar 18, 2020 at 2:57

1 Answer 1

1

You have to make sure about res.data that it has value before calling match. I suggest you use this:

componentDidMount(){
  axios.get('/sites/multiscreen/templates').then(res => {
    if(!!res && 
       !!res.data &&
       !!res.data.template_id &&
       /^[a-z0-9]+$/i.test(res.data.template_id)) 
    {
      this.setState({templates: res.data});
    }
  })
}
Sign up to request clarification or add additional context in comments.

Comments

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.