I am working on an api and when I console.log(data.city) then it gives me result but if I add console.log(data.city.name) then it shows undefined. In console I'm getting the result but after reload this error came.
Also when first console is empty array and then second is data.
const [cityName, setCityName] = useState('delhi');
const [data,setData] = useState([]);
const getWeatherInfo = async () => {
await fetch(
`https://api.openweathermap.org/data/2.5/forecast?q=delhi&units=metric&appid=4beffc863037e89f0f181d893d1cf79b`)
.then(res => res.json())
.then((data)=> {
setData(data)
})
.catch((err)=> {
console.log('error' + err);
})
};
console.log(data.city.name);
useEffect(() => {
getWeatherInfo();
}, []);
