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

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

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 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