JavaScript Displaying the Content

I'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

https://w.trhou.se/s9q3i9sfv6

2 Answers

Steven Parker
Steven Parker
206,336 Points

It 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.

Mohammed Ali
Mohammed Ali
11,126 Points

Thanks Steven - It no longer return array but rather object as you said. Def works, thanks.

Guil Hernandez
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Guil Hernandez
Treehouse Teacher

Hi 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
Aiden Berzins
23,836 Points

Just a note for those coming through to this video much later. This is still a solution that works as of 4/22/19.