Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda