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 Handling Errors

Tucker Blackwell
Tucker Blackwell
3,397 Points

Why is my fetchData function resulting in an error when I chain a .then() method with a console.log()?

The following function runs just fine...

const fetchData = url => { return fetch(url) .then(res => res.json()) .catch(err => console.log("problem: ", err)); }

but when I add a console.log(), it results in "app.js:15 Uncaught (in promise) TypeError: Cannot read property 'message' of undefined at fetchData.then.data"...

const fetchData = url => { return fetch(url) .then(res => console.log(res)) .then(res => res.json()) .catch(err => console.log("problem: ", err)); }

I can't figure out why this is happening? the line the error refers to is the very next instance that I use fetchData...

fetchData("https://dog.ceo/api/breeds/list").then(data => generateOptions(data.message))

1 Answer

Seth Kroger
Seth Kroger
56,414 Points

When you chain then()'s the next then() receives what the previous then() returns. However console.log returns nothing so there is nothing to pass to the next part of the chain.