1

I tried to use this metaweather API but it's not working. this is a react project and I'm tyring on localhost. plz, can anyone show me what am I doing wrong here?

const fetchWeatherData = async() =>{
   fetch('https://www.metaweather.com/api/location/search/?lattlong=36.96,-122.02', {
    method:'GET',
    mode:'no-cors',
    headers: {
      'Content-Type': 'application/json',
    },
    
})
  .then(response => {
    console.log('response',response);
return response.json();
  })
  .then(data => {
    console.log('data',data);
  })
  .catch((err) => {
    console.log(err)
})}

these are logs i got enter image description here

Dila Lola
  • 41
  • 6

1 Answers1

1

You just didn't close the function with curly brackets, I have tested it and it works fine , just call the function fetchWeatherData

const fetchWeatherData = async() => {
    fetch('https://www.metaweather.com/api/location/search/?lattlong=36.96,-122.02', {
     method:'GET',
     mode:'no-cors',
     headers: {
       'Content-Type': 'application/json',
     },
     
 })
   .then(response => {
 return response.json();
   })
   .then(data => {
     console.log('data',data);
   })
   .catch((err) => {
     console.log(err)
    })
}

fetchWeatherData()
Rashed Rahat
  • 2,357
  • 2
  • 18
  • 38
Aian
  • 226
  • 3
  • 10
  • sorry for the missing close curly brackets, but I tried ur code. its same result @Aian – Dila Lola Mar 28 '21 at 08:00
  • SyntaxError: Unexpected end of input and response.status = 0 @Aian – Dila Lola Mar 28 '21 at 08:13
  • aa , you should call it in server side because The MetaWeather API doesn't support CORS, pleae check this one https://stackoverflow.com/questions/58147524/im-trying-to-get-data-from-meta-weather-api-but-its-not-letting-me – Aian Mar 28 '21 at 08:26
  • this code will works fine if called in server side – Aian Mar 28 '21 at 08:28