Проблема с GET запросом к API
При отправке GET запроса на сервер OpenWeatherMap не могу получить ответ (даже 401 ошибки нет). Ключ присутствует, запрос делаю вроде бы правильно, ниже есть вариант который pending выдает, но с этим понятно. Даже со статичными данными ничего не происходит. (даже если в конце запроса написать lang=ru ничего не меняется)
Вот код:
import './App.css';
import {useEffect} from "react";
function App() {
const getWeather = async (city) => {
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=29a0346ff342913fb597eb400c44f143`)
return await response.json()
}
useEffect(() => {
const getCurrentWeather = async () => {
const weather = await getWeather('Moscow')
await console.log(weather)
console.log(weather)
}
getCurrentWeather()
}, [])
return (
<div>test</div>
);
}
export default App;
Ответы (1 шт):
Автор решения: sterx
→ Ссылка
function App() {
const [weather, setWeather] = useState({})
const getWeather = async (city) => {
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=29a0346ff342913fb597eb400c44f143`)
return await response.json()
}
const getCurrentWeather = () => {
getWeather('Moscow')
.then(data => {
setWeather(data)
})
}
if (weather) {
console.log(weather)
}
useEffect(() => {
getCurrentWeather()
}, [])
return (
<div>test</div>
);
}
так?