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

Anthony Ogounchi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Anthony Ogounchi
Full Stack JavaScript Techdegree Graduate 14,594 Points

getting the following error: "TypeError: Cannot read property 'map' of undefine"

I am following exactly the same code as written in the video and I keep getting the following error. Here's my code :

const select = document.getElementById('breeds');
const card = document.querySelector('.card');
const form = document.querySelector('form');

// ------------------------------------------
//  FETCH FUNCTIONS
// ------------------------------------------
fetch('https://dog.ceo/api/breeds/list')
    .then(response => response.json)
    .then(data => generateOptions(data.message))

fetch("https://dog.ceo/api/breeds/image/random")
    .then(response => response.json())
    .then(data => generateImage(data.message))


// ------------------------------------------
//  HELPER FUNCTIONS
// ------------------------------------------
function generateOptions(data) {
    const options = data.map(item => `
    <option value='${item}'>${item}</option>
    `);
    select.innerHTML = options;
}

function generateImage(data) {
    const html = `
  <img src='${data}' alt>
  <p>Click to view image of ${select.value}s</p>
  `;
    card.innerHTML = html;
}

And the error message:

app.js:21 Uncaught (in promise) TypeError: Cannot read property 'map' of undefined
    at generateOptions (app.js:21)
    at app.js:10

1 Answer

In this function:

fetch('https://dog.ceo/api/breeds/list')
    .then(response => response.json)
    .then(data => generateOptions(data.message))

response.json needs to be followed by parentheses

fetch('https://dog.ceo/api/breeds/list')
    .then(response => response.json())
    .then(data => generateOptions(data.message))