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

180
Vistas
How can I get error message of weatherapi using fetch

I'm unable to get an error response for the web app, it displays if there's a valid response but doesn't display anything on a bad request. How can I get the error message either on the console or as json string

Here's the code below

const getWeather = () => {
  let city = document.querySelector("input").value;

  fetch(
    `http://api.weatherapi.com/v1/current.json?key=ca7ec552bc034514a9792135211812&q=${city}&aqi=no`
  )
    .then((data) => data.json())
    .then((data) => displayWeather(data));
};

document.querySelector("button").addEventListener("click", () => {
  getWeather();

  document.querySelector("input").innerText = "";
});

const displayWeather = (data) => {
  const localInfo = data.location.localtime;
  const name = data.location.name;
  const icon = data.current.condition.icon;
  const text = data.current.condition.text;
  const temp = data.current.temp_c;
  const humidity = data.current.humidity;
  const country = data.location.country;
  const windSpeed = data.current.wind_kph;
  const code = data.current.condition.code;
  const error =data.error.message;
  console.log(name, icon, text, temp, humidity, country, windSpeed, code, error);

Live code at this time of writing this: https://github.com/samuelajala01/my-weather-app/blob/master/script.js

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

0

The weather API will only send error sub object if there is error, otherwise it will just send current and location sub objects, hence you just had to add a check for existence of error object in response data

Something like this :

document.querySelector("button").addEventListener("click", () => {
    getWeather();
  
    document.querySelector("input").innerHTML = " ";
  });

  const displayWeather = (data) => {
    if (data.error) {
        alert(data.error.message);
    } else {
        const localInfo = data.location.localtime;
        const name = data.location.name;
        const icon = data.current.condition.icon;
        const text = data.current.condition.text;
        const temp = data.current.temp_c;
        const humidity = data.current.humidity;
        const country = data.location.country;
        const windSpeed = data.current.wind_kph;
        const code = data.current.condition.code;
        console.log(name, icon, text, temp, humidity, country, windSpeed, code);
    }
}

const getWeather = () => {
    let city = document.querySelector("input").value;
  
    fetch(
      `http://api.weatherapi.com/v1/current.json?key=ca7ec552bc034514a9792135211812&q=${city}&aqi=no`
    )
      .then((data) => data.json())
      .then((data) => displayWeather(data))
  };
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