Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
cause of TypeError: Cannot read properties of undefined

I'm trying to fetch some data from an API. The below code "works" when I log the results to console like console.log(liveInfo.tracks), but when I try to do console.log(liveInfo.tracks.current) it fails with this error: TypeError: Cannot read properties of undefined (reading 'current'). Isn't liveInfo.tracks.current how one would normally access the key-value pair?

componentDidMount() {
 fetch('https://kchungradio.airtime.pro/api/live-info-v2')
 .then(res => res.json())
 .then(
  (result) => {
   this.setState({
    isLoaded: true,
    liveInfo: result 
   })
  }
 )
}

The json looks more or less like this:

{
 "station": {
  "env": "production",
 },
 "tracks": {
  "previous": {
   "name": "a",
   "metadata": {
    "id": 1,
   },
  },
  "current": {
   "name": "b",
   "metadata": {
    "id": 2,
   }
  }
 }
}
   
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Because at some point liveInfo.tracks was undefined

Although there is a lack of hints, a common mistake when fetching data from lifecycle is trying to retrieve the value through the state before setData occurs.
Before you use liveInfo, make sure that data fetching is finished

like this

class SomeComponent = {
render() {
if(!this.state.liveInfo?.tracks?.current) return null
....
}

}
about 4 years ago · Juan Pablo Isaza Report

0

It looks you are trying to access to the current before it is filled on the componentDidMount, it means before the fetch has been performed. I suggest you to initialize the state like this:

  state = {
    isLoaded: false,
    liveInfo: {
      tracks: {
        curent: {}
      }
    }
  };

So you will be able to access the current property inside tracks without facing the TypeError. I made a codesandbox, so you can check an example there.

If this does not solve your problem, please let me know ;)

about 4 years ago · Juan Pablo Isaza Report

0

Your call looks right, another way to get the value is console.log(liveInfo.tracks["current"]);

but I think your tracks has no value at runtime. Maybe you can show us more code where you are call console.log.

Maybe you run twice in your statement and at first time it is undefined and throw the error. So add a null check like this console.log(liveInfo?.tracks?.current);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!