Hi I'm testing this JS function using OpenWeatherMap API, but when I fetch the request the promise jumps to .then() with pending status, but the API is answering with HTML code.
Here's my code:
function onSearch(city){
fetch(`api.openweathermap.org/data/2.5/weather?q=London&appid=${ApiKey}&units=metric`)
.then(response => response.json())
.then(data => {
console.log(data)
if(data.main !== undefined){
const city = {
min: Math.round(data.main.temp_min),
max: Math.round(data.main.temp_max),
id: data.id,
img: data.weather[0].icon,
wind: data.wind.speed,
temp: data.main.temp,
name: data.name,
logitude: data.coord.lon,
latitude: data.coord.lat
};
}
})
.catch(e => console.log(e))
}
Here's The API response
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="/manifest.json" />
<!--
Notice the use of in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>Weather App</title>
<script defer src="/static/js/bundle.js"></script></head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
And actually this is my index.html code
Any ideas on how to solve this issue?
CodePudding user response:
OpenWeatherMap does not allow cross origin resource sharing. You can circumvent this problem by using a CORS proxy:
fetch(`https://cors-anywhere.herokuapp.com/api.openweathermap.org/data/2.5/weather?q=London&appid=${ApiKey}&units=metric`)
CodePudding user response:
Checking in depth my code is was missing the "http://" at the begining of the URL when fetching
CodePudding user response:
Its because your function onSearch finishes executing while you call to api havent received results, so solve this you can wait till the results are returned and then finish executing function just with few tricks
change your function to async as below
async function onSearch(city) {
await fetch(`api.openweathermap.org/data/2.5/weather?q=London&appid=${ApiKey}&units=metric`)
.then(response => response.json())
.then(data => {
console.log(data)
if(data.main !== undefined){
const city = {
min: Math.round(data.main.temp_min),
max: Math.round(data.main.temp_max),
id: data.id,
img: data.weather[0].icon,
wind: data.wind.speed,
temp: data.main.temp,
name: data.name,
logitude: data.coord.lon,
latitude: data.coord.lat
};
}
})
.catch(e => console.log(e))
}
and on the calling function never to use await key word as below
async function Calling(){
await onSearch('city name');
}
