简体   繁体   English

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

[英]Unhandled Rejection (TypeError): Cannot read property of undefined

I can't find out why i can't get the data, while I can access it through console.log() ! 我无法找到为什么我无法通过console.log()访问数据的原因!

export class InnerTicket extends React.Component{
constructor(props){
    super(props);
    this.state = {
        ticket: ''
    }
}

componentDidMount() {
    this.getTicket().then(result => this.setState({
        ticket: result
    }))
}

getTicket(){
    const slug = this.props.match.params.id;
    return this.props.TicketsStore.loadTicket(slug);
}
render(){
    console.log(this.state);

everything works fine when I run this and i can see the data through the console: 当我运行此命令时一切正常,我可以通过控制台查看数据:

{ticket: {…}}ticket: {success: true, data: {…}, error: "", message: ""}data: {result: "success", ticketid: 22, tid: "168552", c: "WHgCsCBO", deptid: 5, …}error: ""message: ""success: true__proto__: Object__proto__: Object

but when I want to display my data in the view like this: 但是当我想在视图中显示我的数据时,如下所示:

render(){
    return( <span className="label label-warning">
                            {this.state.ticket.data.status}
                        </span>)

I get this error: TypeError: Cannot read property 'status' of undefined 我收到此错误:TypeError:无法读取未定义的属性“状态”

You're initialized your state to this: 您已将状态初始化为:

this.state = {
    ticket: ''
}

So on the first render, this.state.ticket is an empty string, and this.state.ticket.data is undefined. 因此,在第一个渲染中,this.state.ticket是一个空字符串,并且this.state.ticket.data是未定义的。 Only later are you updating state.ticket to an object. 直到稍后,您才将state.ticket更新为一个对象。

To fix this, either make the initial state be something that works with your render method, or make your render method check for the possibility of an empty string. 要解决此问题,请将初始状态设置为可与您的render方法一起使用的状态,或者使render方法检查是否存在空字符串。

I'm not sure if it's perfect, but should work fine: 我不确定它是否完美,但是应该可以正常工作:

render(){
    return( <span className="label label-warning">
                            {this.state.ticket.data.status ? this.state.ticket.data.status : null}
                        </span>)

暂无
暂无

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

相关问题 未处理的拒绝(TypeError):无法读取未定义的属性“子级” - Unhandled Rejection (TypeError): Cannot read property 'children' of undefined React-未处理的拒绝(TypeError):无法读取未定义的属性“ city” - React- Unhandled Rejection (TypeError): Cannot read property 'city' of undefined 未处理的拒绝(TypeError):无法读取未定义的 Stripe Javascript 的属性“id” - Unhandled Rejection (TypeError): Cannot read property 'id' of undefined Stripe Javascript ReactJS:未处理的拒绝(TypeError):无法读取未定义的属性“推送” - ReactJS: Unhandled Rejection (TypeError): Cannot read property 'push' of undefined 反应未处理的拒绝(TypeError):无法读取未定义的属性“状态” - React Unhandled Rejection (TypeError): Cannot read property 'state' of undefined 用于 React 的 geolib - 未处理的拒绝(TypeError):无法读取未定义的属性“getCenter” - geolib for React - Unhandled Rejection (TypeError): Cannot read property 'getCenter' of undefined 未处理的拒绝(TypeError):无法读取未定义 MERN 的属性“长度” - Unhandled Rejection (TypeError): Cannot read property 'length' of undefined MERN 未处理的拒绝(TypeError):无法读取未定义的属性“isUnAuthorizedError” - Unhandled Rejection (TypeError): Cannot read property 'isUnAuthorizedError' of undefined 未处理的拒绝(TypeError):无法读取未定义的属性“替换” - Unhandled Rejection (TypeError): Cannot read property 'replace' of undefined axios:未处理的拒绝(TypeError):无法读取未定义的属性“错误” - axios: Unhandled Rejection (TypeError): Cannot read property 'error' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM