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
javascript reactjs
add a comment |
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
javascript reactjs
what aboutrender(){ 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
add a comment |
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
javascript reactjs
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
javascript reactjs
asked 17 hours ago
Ali Djawadi
65
65
what aboutrender(){ 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
add a comment |
what aboutrender(){ 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
add a comment |
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.
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
add a comment |
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>)
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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>)
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
add a comment |
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>)
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
add a comment |
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>)
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>)
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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