Jump to content

my javascript/node error message code isnt working

idjhalo
Go to solution Solved by PyroTheWise,

@idjhalo So you are wanting it to fail when the web-site address is not valid right?

 

If so, then I did a quick nslookup on wwwteamtreehouse.com and it is a registered URL:

 

image.png.b4cfab9d37b538980f4f7cc255085b58.png

 

I am thinking that is your issue and not your code.

so I'm trying to create an error event handler in javascript. I'm purposely adding the website wrong so that I get a specific error message but thats not what I'm getting. When I run my code I get the following:

 

undefined:1

 

 

 

SyntaxError: Unexpected end of JSON input

    at JSON.parse (<anonymous>)

    at IncomingMessage.<anonymous> (/Users/elmaschingon/Desktop/app.js:24:30)

    at IncomingMessage.emit (events.js:327:22)

    at endReadableNT (_stream_readable.js:1201:12)

    at processTicksAndRejections (internal/process/task_queues.js:84:21)

 

Here is my code below

 

// Problem: We need a simple way to look at a user's badge count and JavaScript points
// Solution: Use Node.js to connect to Treehouse's API to get profile information to print out


const https = require('https');

// Function: to pring message to console
function printMessage (username, badgeCount, points) {
  const message = `${username} has ${badgeCount} total badge(s) and ${points} points in Javascript.`
  console.log(message);
}

function getProfile(username) {
  // Connect to the API url 
  const request = https.get(`https://wwwteamtreehouse.com/${username}.json`, response => {
      let body = "";
      // Read the data
      response.on('data', data => {
        body += data.toString();
      });

      // Parse the data
        response.on('end', () => {
        const profile = JSON.parse(body);
        printMessage(username, profile.badges.length, profile.points.JavaScript );
        });

    });
  request.on('error', error => console.error(`Problem with request: ${error.message}`));
}

const users = process.argv.slice(2)
users.forEach(getProfile);

The really weird thing is that when I make the site "https://wwwwteamtreehouse.com" (adding an additional w and still missing the '.') I get the right error code that I'm looking for which is:

 

Problem with request: getaddrinfo ENOTFOUND wwwwteamtreehouse.com

 

Any help would be GREATLY appreciated guys

Link to comment
Share on other sites

Link to post
Share on other sites

@idjhalo So you are wanting it to fail when the web-site address is not valid right?

 

If so, then I did a quick nslookup on wwwteamtreehouse.com and it is a registered URL:

 

image.png.b4cfab9d37b538980f4f7cc255085b58.png

 

I am thinking that is your issue and not your code.

Link to comment
Share on other sites

Link to post
Share on other sites

@pyroTheWise

Yeah apparently it IS! lol smh.

the tutorial video I'm watching must be older and they must have registered that after the fact? idk but in the tutorial the instructor used www and it was giving him the proper code so given that I was practically copy and pasting i thought the issue was my code. thank you so much. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×