Undefined Nested Object in REST API with React Hooks? mitchelln11 on June 04, 2020 Having trouble trying to access nested objects in the OpenWeather REST API. Getting full results in Postman, but not when trying to render. Postma... Read full post Submit Preview Dismiss Collapse Expand Diego Sisto Diego Sisto Diego Sisto Follow Location Argentina Work Developer at Xapo Joined Mar 23, 2020 • Jun 4 '20 Dropdown menu Copy link Hide It's most likely something silly. Try console.log(openWeather, openWeather.weather, openWeather.weather[0]) Collapse Expand mitchelln11 mitchelln11 mitchelln11 Follow I have experience in the front-end (HTML, CSS, JavaScript), and I just learned C# and ASP.NET. Attempting React now. Location Wisconsin Work Developer Joined Feb 25, 2020 • Jun 4 '20 Dropdown menu Copy link Hide comes back undefined. Collapse Expand Diego Sisto Diego Sisto Diego Sisto Follow Location Argentina Work Developer at Xapo Joined Mar 23, 2020 • Jun 4 '20 Dropdown menu Copy link Hide You should have 3 outputs. Is that undefined, undefined, undefined? Can we get the full code? mitchelln11 mitchelln11 mitchelln11 Follow I have experience in the front-end (HTML, CSS, JavaScript), and I just learned C# and ASP.NET. Attempting React now. Location Wisconsin Work Developer Joined Feb 25, 2020 • Jun 4 '20 Dropdown menu Copy link Hide Unhandled Rejection (TypeError): Cannot read property '0' of undefined. Breaks the page, doesn't even log anything. Diego Sisto Diego Sisto Diego Sisto Follow Location Argentina Work Developer at Xapo Joined Mar 23, 2020 • Jun 4 '20 Dropdown menu Copy link Hide Can we see the code please? It could be a number of things. mitchelln11 mitchelln11 mitchelln11 Follow I have experience in the front-end (HTML, CSS, JavaScript), and I just learned C# and ASP.NET. Attempting React now. Location Wisconsin Work Developer Joined Feb 25, 2020 • Jun 5 '20 Dropdown menu Copy link Hide It is in React and uses hooks. import React, { useState, useEffect } from 'react'; import axios from 'axios'; const LocalWeather = () => { const [openWeather, setWeather] = useState([]); useEffect(() => { axiosGet(); }, []); // Run once on load const axiosGet = () => { const data = axios.get(`https://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=${process.env.REACT_APP_WEATHER_KEY}`) .then(data => setWeather(data.data)); console.log(openWeather, openWeather.weather, openWeather.weather[0]); } return ( <ul> <li>{openWeather.name}</li> <li>{openWeather.cod}</li> <li>{openWeather.id}</li> <li>{openWeather.timezone}</li> <li>{openWeather.dt}</li> <li>{openWeather.visibility}</li> <li>{openWeather.base}</li> {/* <li>{openWeather.current.weather[0].main}</li> */} </ul> ); } export default LocalWeather; The console.log in the axiosGet method is breaking the page. Code of Conduct • Report abuse
It's most likely something silly. Try
console.log(openWeather, openWeather.weather, openWeather.weather[0])comes back undefined.
You should have 3 outputs. Is that
undefined, undefined, undefined?Can we get the full code?
Unhandled Rejection (TypeError): Cannot read property '0' of undefined. Breaks the page, doesn't even log anything.
Can we see the code please? It could be a number of things.
It is in React and uses hooks.
The console.log in the axiosGet method is breaking the page.