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

Getting error when viewing username/profile.

Works OK when starting node app.js. But when I add the username to the URL, I get an error.

This is the error

Server running at http://<workspace-url>/                                                                                                      
/home/treehouse/workspace/profile.js:23                                                                                                        
or getting the profile for " + username + ". (" + https.STATUS_CODES[response.                                                                 
                                                                    ^                                                                          
TypeError: Cannot read property '404' of undefined                                                                                             
    at ClientRequest.<anonymous> (/home/treehouse/workspace/profile.js:23:137)                                                                 
    at ClientRequest.g (events.js:199:16)                                                                                                      
    at ClientRequest.emit (events.js:107:17)                                                                                                   
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:426:21)                                                              
    at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)                                                                             
    at TLSSocket.socketOnData (_http_client.js:317:20)                                                                                         
    at TLSSocket.emit (events.js:107:17)                                                                                                       
    at readableAddChunk (_stream_readable.js:163:16)                                                                                           
    at TLSSocket.Readable.push (_stream_readable.js:126:10)                                                                                    
    at TCP.onread (net.js:538:20)    

can you show us the code for this route?

If I recall, one of the usernames that was listed in the video wasn't valid anymore, which would return a 404 (not found) if you tried to browse to it. However, this one is valid: http://teamtreehouse.com/chalkers.json so make sure that one is working for you first, then try to pick a few more to add to the list (try them in a browser first to make sure).

This is the route for username (excluding module.exports...etc):

// handle http route  get /: username i.e. /chalkers
function user(request, response){
  //if url == "/..." 
  var username = request.url.replace("/", "");
  if(username.length > 0) {
    response.writeHead (200, {'Content-Type': 'text/plain'});
    response.write("Header\n");

    // get json from Treehouse
       var studentProfile = new Profile(username);
      // on end
      studentProfile.on("end", function(profileJSON){
      // show profile      

      // store the values which we need
        var values = {
          avatarURL: profileJSON.gravatar_url,
          username: profileJSON.profile_name,
          badges: profileJSON.badges.length,
          javascriptPoints: profileJSON.points.JavaScript 
        }
        //simple response
         response.write(values.username + " has " + values.badges + " badges\n");
         response.end("Footer\n");
      });

      // on error    
      studentProfile.on("error", function(error){
      //show error   
      response.end("Footer\n");
    });

  }
}

I'm searching for my own profile, so it's not an issue of using a username that doesn't exist. Sorry for the updates, the formatting was crazy.

I think the error lies in "request.url.replace("/", ""); "

I'd console.log(username) after you define it to make sure it's coming out as you expect. It looks like it's coming out as "https.STATUS_CODES[response."... (the message cuts off)

Line 23 of profiles.js is what is throwing the error, I think it's probably expecting a username, but is being fed an error instead.

Hi James, yes, that seems to be the issue, but I can't figure out what's wrong. I'll keep investigating, I guess....

Edit: No, that is not the issue.

Also, FYI - your answers are going in the comments. Can't get credit for best answer in Comments and cant' get upvotes.

Ahh but nothing has been answered yet =)

Can you provide the full URL you are going to?

1 Answer