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

Response Code won't properly display when using response.statusCode

Hello everyone,

I'm having some issues getting the response.statusCode to function properly in the else statement of my code. When this code is run I get the response 200:

   if (!error && response.statusCode == 200) {
            console.log(`Status Code: ${response.statusCode}`);

When this section of the code runs I get " Cannot read property 'statusCode' of undefined."

   } else {
            console.log(`Status Code: ${response.statusCode}`);

This is the full section of code:

request(allShirtURL, (error, response, body) => {
        if (!error && response.statusCode == 200) {
            console.log(`Status Code: ${response.statusCode}`);
            resolve(body);
            console.log('Successfully connected to http://shirts4mike.com/shirts.php');
        } else {
            console.log(`Status Code: ${response.statusCode}`);
            reject(error);
        }
    })
});

Any thoughts? Thanks everyone!