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

123
Visualizações
Accessing array element from response data using react-query

I am using react-query to make a GET request to my API in which the response is an array of objects. Each object has 1 property which is the name that I want to access. When I log the response to the console, it appears like the image listed at the bottom. Even says the prototype is an array. However, when I try to access an element of the response, it says "cannot read properties of undefined".

Here is the code which displays the data as shown in the image:

const { isLoading, error, data } = useQuery('cat_names', () =>
        fetch('<api-link>').then(res =>
            res.json()
        )
    )
console.log(data)

Here is the code I used to try accessing the first element of the array.

const { isLoading, error, data } = useQuery('cat_names', () =>
   fetch('<api-link>').then(res =>
       res.json()
   )
)
console.log(data[0])

I have also tried mapping data, with no luck:

const mapped_data = data.map(element => element.name)
console.log(mapped_data)

enter image description here

about 4 years ago · Juan Pablo Isaza
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