Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
36
rated 0 times [  42] [ 6]  / answers: 1 / hits: 33625  / 6 Years ago, mon, july 9, 2018, 12:00:00
TypeError: Cannot read property 'length' of undefined

That's what the compiler says when I run my react app. What I do need to do with this?


request = (start,end) => {
if(this.state.teams.length < 1){
axios.get(`${ URL }/teams`)
.then( response => {
this.setState({
teams:response.data
})
})
}

axios.get(`${ URL }/articles?_start=${start}&_end=${end}`)
.then( response => {
this.setState({
items:[...this.state.items,...response.data]
})
})
}

More From » reactjs

 Answers
57

I would suggest to check first if the props is undefined or empty or even declared.



for example:-



    const card = props && props.cards && props.cards.length > 0 ?
props.cards.map((card, i) => {
return (
<card >
)
}) : '';


And return your card.


[#54024] Thursday, July 5, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janeth

Total Points: 498
Total Questions: 91
Total Answers: 89

Location: Malaysia
Member since Wed, May 11, 2022
2 Years ago
;