Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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))