Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在react应用程序上使用regex的match()获取错误_Javascript_Regex_Reactjs_Match - Fatal编程技术网

Javascript 在react应用程序上使用regex的match()获取错误

Javascript 在react应用程序上使用regex的match()获取错误,javascript,regex,reactjs,match,Javascript,Regex,Reactjs,Match,我得到的错误是: 未处理的拒绝(TypeError):无法读取未定义的属性“match” 感谢您的帮助 在调用match之前,必须确保res.data具有值。我建议你用这个: componentDidMount(){ axios.get('/sites/multiscreen/templates').then(res => { if(res.data.template_id.match(/^[a-z0-9]+$/i)){ this.setState({

我得到的错误是:

未处理的拒绝(TypeError):无法读取未定义的属性“match”


感谢您的帮助

在调用
match
之前,必须确保
res.data
具有值。我建议你用这个:

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

听起来好像
template\u id
不是字符串。它是JSON。。。如何将match()与json一起使用?把它转换成字符串?你能上传你的
res.data
?错误是
res.data.template\u id
未定义。
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});
    }
  })
}