Axios Unhandled Rejection (Typeerror): Cannot Read Property 'data' of Undefined
Discussion on: Undefined Nested Object in REST API with React Hooks?
Replies for: You should have 3 outputs. Is that undefined, undefined, undefined? Can we get the total lawmaking?
Source: https://dev.to/mitchelln11/comment/100kc
mitchelln11
I accept feel in the front-stop (HTML, CSS, JavaScript), and I just learned C# and ASP.NET. Attempting React now.
Location
Wisconsin
Work
Developer
Joined
•
Unhandled Rejection (TypeError): Cannot read belongings '0' of undefined. Breaks the folio, doesn't even log anything.
Can we meet the code please? Information technology could be a number of things.
mitchelln11
I have experience in the front-finish (HTML, CSS, JavaScript), and I just learned C# and ASP.NET. Attempting React now.
Location
Wisconsin
Work
Developer
Joined
•
It is in React and uses hooks.
The panel.log in the axiosGet method is breaking the page.
Thank you!
Let'south change the
panel.log(openWeather, openWeather.atmospheric condition, openWeather.weather[0]);
to be inside thethen
callback:That volition give y'all an idea of what'south really stored in
openWeather
.On the other hand, you got a
current
in the failing line..current
is used for react ref simply not for land. Attempt removing that.Good luck!
mitchelln11
I have experience in the forepart-end (HTML, CSS, JavaScript), and I just learned C# and ASP.NET. Attempting React now.
Location
Wisconsin
Piece of work
Developer
Joined
•
Become the following:
I also removed the electric current. Thanks, I forgot I had that in there.
Seem similar you get an empty array. Maybe yous are getting an unexpected response, check chrome network console developers.google.com/web/tools/ch...
Maybe y'all don't demand to
data.information
and butdata
. Keep console logging =)mitchelln11
I take feel in the front-stop (HTML, CSS, JavaScript), and I simply learned C# and ASP.NET. Attempting React at present.
Location
Wisconsin
Piece of work
Developer
Joined
•
And then somebody on a Slack channel figured this out for me.
When returning JSX:
This feels like a problems to me, but information technology does output the response from the REST API.
I am still getting a alarm, which I am unsure why:
It'south saying
Line 14:15: 'data' is assigned a value just never used no-unused-vars
, although everything works. Aren't I setting the rest endpoint to data, and then using information technology when running the setWeather method?Great that you figure it out!
Keep in mind that yous get a commencement render earlier y'all go the data with
openWeather
as the default value passed inuseState
.Maybe you can:
no-unused-vars
is because yous are doingconst data =
and never utilize that data const. Non to exist confused with theso
data
argument. Note I removed information technology in my casemitchelln11
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
•
Sweetness, works perfectly, i idea that data was the same thing as in the
and then
statement. Works, no errors! Give thanks yous so much for your helpGlad to read that! Go on upward the good work!
Gustavo Zapata
Passionate well-nigh the design and evolution of figurer programs
Location
London
Education
Computer Science BSc
Piece of work
Software engineer at Cisco
Joined
•
I simply spent hours trying to figure out what the upshot was. Thanks to y'all guys (mitchelln11 and Diego Sisto) I was able to set up information technology.
My ii cents:
Instead of if(!openWeather) return zip;
I used {!openWeather && openWeather.weather.map(...) which will render the rest of the app and just ignoring this line.