Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

90
Vistas
undefined error when fetching weather forecast api javascript

I am trying to fetch the weather forecast api json like I did with the current weather api, but it does not seem to work any way I try.

    let inOneDay = {
fetchWeather: function(){
    fetch("https://api.openweathermap.org/data/2.5/forecast?q=Dortmund&units=metric&cnt=1&appid=758fce6dd3722cf25cd213a13bbc5484"
    ).then(resp => resp.json())
    .then(data => console.log(data));
}

};

I have no idea where I went wrong. I used the same logic to make the code below work:

let weather = {
    fetchWeather: function(){
        fetch("https://api.openweathermap.org/data/2.5/weather?q=Dortmund&units=metric&appid=758fce6dd3722cf25cd213a13bbc5484"
        ).then((response) => response.json())
        .then((data) => this.displayWeather(data));
    },
    displayWeather: function(data){
        const{icon,description} = data.weather[0];
        const{temp} = data.main;
        document.querySelector(".icon").src = "https://www.openweathermap.org/img/wn/" + icon + ".png";
        document.querySelector(".celsius").innerText = Math.round(temp) + "°C";
        document.querySelector(".desc").innerText = description;
    }

}

Grateful for any ideas!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Examining the json reply from that API, it looks like the OP code expects different fields than the service provides.

const result = 
{"cod":"200","message":0,"cnt":1,"list":[{"dt":1631577600,"main":{"temp":13.31,"feels_like":13.05,"temp_min":13.31,"temp_max":15.87,"pressure":1018,"sea_level":1018,"grnd_level":1007,"humidity":90,"temp_kf":-2.56},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":95},"wind":{"speed":1.42,"deg":94,"gust":2.29},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2021-09-14 00:00:00"}],"city":{"id":2935517,"name":"Dortmund","coord":{"lat":51.5167,"lon":7.45},"country":"DE","population":588462,"timezone":7200,"sunrise":1631595820,"sunset":1631641686}};

const result0 = result.list[0];        // notice .list[0]
const weather0 = result0.weather[0];   // notice weather[0]
const main = result0.main;             // notice main is a sibling prop to weather
const temp = main.temp

console.log(`weather is ${JSON.stringify(weather0)}`);
console.log(`main is ${JSON.stringify(main)}`);
console.log(`temp is ${temp}`);

Be sure to check for errors before dereferencing the result. It also looks like the api provides a cnt prop which might indicate the number of elements in the list.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda