Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Frederick Alcantara
Frederick Alcantara
7,617 Points

How come I can't access an object even though it exists

I am using jquery ajax and json to retrieve information from an API. When the information is shown, it is unable to retrieve two properties from 2 objects. I currently can access 4 out of 6 objects with ease. The other two (bran stark and arya stark) I'm having a difficult time with, I can't access their last two properties (Seasons and PlayedBy. I know for a fact that all the I am using and their properties exist, so why can't I access specific properties from specific objects? Everything else I am able to access just not the last two properties.

https://codepen.io/frederickalcantara/pen/zzYmmO

1 Answer

Steven Parker
Steven Parker
231,007 Points

Neither of the problem cases have a spouse.

In the code that gets the "spouse" field, you have these lines:

    if (data.spouse === "") {
       return spouse.html("N/A");

So if there is no spouse, the function returns and the remaining fields of the page are not updated. That return keyword should probably not be there.

There is similar code elsewhere in the function, but it is not executed for any of these particular data records. But if you expand the program to include other records (or if these should change) it could also cause incomplete updates.

Frederick Alcantara
Frederick Alcantara
7,617 Points

That made all the difference in the world lol. I finally got it to work, thank you this took a couple of days to figure out lol.