componentDidMount(){
axios.get('/sites/multiscreen/templates').then(res => {
if(res.data.template_id.match(/^[a-z0-9]+$/i)){
this.setState({
templates: res.data,
});
}
})
}我得到的错误是:
未处理拒绝(TypeError):无法读取未定义属性的“匹配”
任何帮助都是非常感谢的!
发布于 2020-03-18 05:45:12
在调用res.data之前,您必须确保它具有价值。我建议你用这个:
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});
}
})
}https://stackoverflow.com/questions/60732829
复制相似问题