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

188
Visualizações
Uncaught TypeError: Cannot read properties of undefined (reading 'forEach') - OpenWeather

I am working on a weather app project in React as a beginner. I am using OpenWeatherAPI in that case.

My problem is when I try to use forEach, it gives an error as it seems below.

ApiCall.jsx:15 Uncaught TypeError: Cannot read properties of undefined (reading 'forEach')

Here is my Header component:

import ApiCall from './ApiCall';

function Header() {
    const cities = ["İstanbul", "Ankara", "İzmir"]

    return (
        <div>
            <div className="header">
                <select name="selection">
                    <option value="istanbul">{cities[0]}</option>
                    <option value="ankara">{cities[1]}</option>
                    <option value="izmir">{cities[2]}</option>
                </select>
            </div>
            <ApiCall getCities={cities} />
        </div>   
    )

}

export default Header

And this is my ApiCall component:

import axios from "axios"
import { useEffect, useState } from "react"

function ApiCall({ getCities }) {
    const[data, setData] = useState([])
    
    useEffect(() => {
        axios(`https://api.openweathermap.org/data/2.5/forecast?q=${selectCity}&appid=c681e6e33ec339728fdf88e0b24a2a01`)
        .then(res => setData(res.data))
        .catch(err=> console.log(err))
    })
    
    const { city, list } = data
    
    const selectCity = getCities.array.forEach((element) => {
        if (city.name === element) {
            return element
        }
    });

    return (
      null
    )
}

export default ApiCall

All answers will be appreciated.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The foreach should be like this

getCities.forEach

Instead of

getCities.array.forEach

What do you think ?

If getCities is an array , you have to use forEach on it instead of using .array.forEach

about 4 years ago · Santiago Trujillo Relatório

0

It seems that you try to filter all the available cities with the cities you are getting as property to your component (getCities).

Since getCities is an array you can directly call the forEach function on it and loop over the cities but since you want to apply a filter I think you are better off using Javascript's build-in filter method.

const selectCity = getCities.filter((possibleCityName) => {
        return city.name === possibleCityName)
    });
about 4 years ago · Santiago Trujillo 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