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 trialRyan Pierson
14,365 PointsI'm trying to copy the code exactly as it's working in the video, but I'm getting the error "data.map is not a function"
Here is a snapshot of my workspace
2 Answers
Steven Parker
231,271 PointsIt appears that the API for the more extensive "all" list is a bit different from the one used in the video. In the video, the standard list clearly returns an array; but this one seems to be returning an object instead with breed keys and sub-breed arrays as values.
The "map" method is only available on arrays, but here's a revised "generateOptions" function that will work with the object that I fetched using your URL today:
function generateOptions(data) {
let options = "";
for (item in data) {
if (data[item].length > 0) {
data[item].forEach(sb => options += `
<option value='${sb} ${item}'>${sb} ${item}</option>
`);
} else {
options += `
<option value='${item}'>${item}</option>
`;
}
}
select.innerHTML = options;
}
I've alerted the staff about this. I doubt they can change the video but they might add a notice to the teacher's notes about the new API.
Guil Hernandez
Treehouse TeacherHi Ryan Pierson,
It looks like the endpoint shown in the video (and posted in the teacher's notes) still returns the array of breeds. If you remove /all
from your URL it should work: https://dog.ceo/api/breeds/list
.
Thanks for looking into this and your awesome suggestion, Steven Parker! I'll add an instruction step to the workshop regarding the API changes.
Hope this helps. :)
Aiden Berzins
23,837 PointsJust a note for those coming through to this video much later. This is still a solution that works as of 4/22/19.
Mohammed Ali
11,134 PointsMohammed Ali
11,134 PointsThanks Steven - It no longer return array but rather object as you said. Def works, thanks.