Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!