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

Sean Gabriel Villoria
Sean Gabriel Villoria
8,515 Points

Why does the created checkStatus function return a Promise: fulfilled but with an undefined value?

So, I created the code below just like Guil did in the video, and I got an error,

"Uncaught (in promise) TypeError: Cannot read property 'message' of undefined"

Troubleshooting this, I found that commenting out .then(checkStatus) fixed the error. So I added,

console.log(Promise.resolve);

to the checkStatus function. When I ran it again, I got the same error, and console logged:

PromiseΒ {<fulfilled>: undefined} proto: Promise [[PromiseStatus]]: "fulfilled" [[PromiseValue]]: undefined

So I figured out that what's breaking the code is that checkStatus is returning the undefined Promise. My question is why? Is there a way around this? I noticed in the video Guil's console logs a similar error. Is it a problem with the code itself?

Thanks in advance!

Code snippet: function fetchData(url){ return fetch(url) .then(checkStatus)
.then(res => res.json()) .catch(error => console.log('Looks like there was a problem', error)) }

function checkStatus (response) { if(response.ok) { console.log(Promise.resolve()); return Promise.resolve(); } else { return Promise.reject(new Error(response.statusText)) } }

1 Answer

Sean Gabriel Villoria
Sean Gabriel Villoria
8,515 Points

I figured it out. I didn't call response in my Promise.resolve. This works now. :)

function checkStatus (response) { if(response.ok) { console.log(Promise.resolve(response)); return Promise.resolve(response); } else { return Promise.reject(new Error(response.statusText)) } }