Unhandled Rejection (TypeError): Cannot read property of undefined











up vote
0
down vote

favorite












I can't find out why i can't get the data, while I can access it through 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










share|improve this question






















  • what about render(){ return( <span className="label label-warning"> {this.state.ticket.data ? this.state.ticket.data.status : null} </span>)
    – ingvar
    16 hours ago










  • @ingvar the same error occurs...
    – Ali Djawadi
    16 hours ago










  • oops. {this.state.ticket ? this.state.ticket.data.status : null} should work
    – ingvar
    16 hours ago















up vote
0
down vote

favorite












I can't find out why i can't get the data, while I can access it through 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










share|improve this question






















  • what about render(){ return( <span className="label label-warning"> {this.state.ticket.data ? this.state.ticket.data.status : null} </span>)
    – ingvar
    16 hours ago










  • @ingvar the same error occurs...
    – Ali Djawadi
    16 hours ago










  • oops. {this.state.ticket ? this.state.ticket.data.status : null} should work
    – ingvar
    16 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I can't find out why i can't get the data, while I can access it through 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










share|improve this question













I can't find out why i can't get the data, while I can access it through 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







javascript reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 17 hours ago









Ali Djawadi

65




65












  • what about render(){ return( <span className="label label-warning"> {this.state.ticket.data ? this.state.ticket.data.status : null} </span>)
    – ingvar
    16 hours ago










  • @ingvar the same error occurs...
    – Ali Djawadi
    16 hours ago










  • oops. {this.state.ticket ? this.state.ticket.data.status : null} should work
    – ingvar
    16 hours ago


















  • what about render(){ return( <span className="label label-warning"> {this.state.ticket.data ? this.state.ticket.data.status : null} </span>)
    – ingvar
    16 hours ago










  • @ingvar the same error occurs...
    – Ali Djawadi
    16 hours ago










  • oops. {this.state.ticket ? this.state.ticket.data.status : null} should work
    – ingvar
    16 hours ago
















what about render(){ return( <span className="label label-warning"> {this.state.ticket.data ? this.state.ticket.data.status : null} </span>)
– ingvar
16 hours ago




what about render(){ return( <span className="label label-warning"> {this.state.ticket.data ? this.state.ticket.data.status : null} </span>)
– ingvar
16 hours ago












@ingvar the same error occurs...
– Ali Djawadi
16 hours ago




@ingvar the same error occurs...
– Ali Djawadi
16 hours ago












oops. {this.state.ticket ? this.state.ticket.data.status : null} should work
– ingvar
16 hours ago




oops. {this.state.ticket ? this.state.ticket.data.status : null} should work
– ingvar
16 hours ago












2 Answers
2






active

oldest

votes

















up vote
0
down vote













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. Only later are you updating state.ticket to an object.



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.






share|improve this answer





















  • how do I make my render method check for the possibility of an empty string?
    – Ali Djawadi
    16 hours ago










  • For example, if you want to render nothing: if (this.state.ticket === '') { return null }
    – Nicholas Tower
    16 hours ago












  • it wont work...
    – Ali Djawadi
    16 hours ago










  • it will, have you tried?
    – xadm
    16 hours ago


















up vote
0
down vote













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





share|improve this answer





















  • no, the result is the same: Cannot read property 'status' of undefined
    – Ali Djawadi
    16 hours ago










  • What about {this.state.ticket ? this.state.ticket.data.status : null}
    – Bruinen
    16 hours ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139664%2funhandled-rejection-typeerror-cannot-read-property-of-undefined%23new-answer', 'question_page');
}
);

Post as a guest
































2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













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. Only later are you updating state.ticket to an object.



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.






share|improve this answer





















  • how do I make my render method check for the possibility of an empty string?
    – Ali Djawadi
    16 hours ago










  • For example, if you want to render nothing: if (this.state.ticket === '') { return null }
    – Nicholas Tower
    16 hours ago












  • it wont work...
    – Ali Djawadi
    16 hours ago










  • it will, have you tried?
    – xadm
    16 hours ago















up vote
0
down vote













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. Only later are you updating state.ticket to an object.



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.






share|improve this answer





















  • how do I make my render method check for the possibility of an empty string?
    – Ali Djawadi
    16 hours ago










  • For example, if you want to render nothing: if (this.state.ticket === '') { return null }
    – Nicholas Tower
    16 hours ago












  • it wont work...
    – Ali Djawadi
    16 hours ago










  • it will, have you tried?
    – xadm
    16 hours ago













up vote
0
down vote










up vote
0
down vote









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. Only later are you updating state.ticket to an object.



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.






share|improve this answer












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. Only later are you updating state.ticket to an object.



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.







share|improve this answer












share|improve this answer



share|improve this answer










answered 17 hours ago









Nicholas Tower

6,0851615




6,0851615












  • how do I make my render method check for the possibility of an empty string?
    – Ali Djawadi
    16 hours ago










  • For example, if you want to render nothing: if (this.state.ticket === '') { return null }
    – Nicholas Tower
    16 hours ago












  • it wont work...
    – Ali Djawadi
    16 hours ago










  • it will, have you tried?
    – xadm
    16 hours ago


















  • how do I make my render method check for the possibility of an empty string?
    – Ali Djawadi
    16 hours ago










  • For example, if you want to render nothing: if (this.state.ticket === '') { return null }
    – Nicholas Tower
    16 hours ago












  • it wont work...
    – Ali Djawadi
    16 hours ago










  • it will, have you tried?
    – xadm
    16 hours ago
















how do I make my render method check for the possibility of an empty string?
– Ali Djawadi
16 hours ago




how do I make my render method check for the possibility of an empty string?
– Ali Djawadi
16 hours ago












For example, if you want to render nothing: if (this.state.ticket === '') { return null }
– Nicholas Tower
16 hours ago






For example, if you want to render nothing: if (this.state.ticket === '') { return null }
– Nicholas Tower
16 hours ago














it wont work...
– Ali Djawadi
16 hours ago




it wont work...
– Ali Djawadi
16 hours ago












it will, have you tried?
– xadm
16 hours ago




it will, have you tried?
– xadm
16 hours ago












up vote
0
down vote













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





share|improve this answer





















  • no, the result is the same: Cannot read property 'status' of undefined
    – Ali Djawadi
    16 hours ago










  • What about {this.state.ticket ? this.state.ticket.data.status : null}
    – Bruinen
    16 hours ago















up vote
0
down vote













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





share|improve this answer





















  • no, the result is the same: Cannot read property 'status' of undefined
    – Ali Djawadi
    16 hours ago










  • What about {this.state.ticket ? this.state.ticket.data.status : null}
    – Bruinen
    16 hours ago













up vote
0
down vote










up vote
0
down vote









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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered 16 hours ago









Bruinen

222




222












  • no, the result is the same: Cannot read property 'status' of undefined
    – Ali Djawadi
    16 hours ago










  • What about {this.state.ticket ? this.state.ticket.data.status : null}
    – Bruinen
    16 hours ago


















  • no, the result is the same: Cannot read property 'status' of undefined
    – Ali Djawadi
    16 hours ago










  • What about {this.state.ticket ? this.state.ticket.data.status : null}
    – Bruinen
    16 hours ago
















no, the result is the same: Cannot read property 'status' of undefined
– Ali Djawadi
16 hours ago




no, the result is the same: Cannot read property 'status' of undefined
– Ali Djawadi
16 hours ago












What about {this.state.ticket ? this.state.ticket.data.status : null}
– Bruinen
16 hours ago




What about {this.state.ticket ? this.state.ticket.data.status : null}
– Bruinen
16 hours ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139664%2funhandled-rejection-typeerror-cannot-read-property-of-undefined%23new-answer', 'question_page');
}
);

Post as a guest




















































































這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud