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 Build a Simple Dynamic Site with Node.js Creating a Basic Template Engine in Node.js Reading from Files

Olu Gbadebo
PLUS
Olu Gbadebo
Courses Plus Student 516 Points

Console error

When I run the app.js, I get a TypeError message as follows

" TypeError: Cannot read property 'on' of undefined "

and the console points to the '.on' method

" /home/treehouse/workspace/router.js:46
studentProfile.on("error", function(error){
^ "

1 Answer

Erik Nuber
Erik Nuber
20,629 Points

I believe that on is a jquery method so you would have to have jquery linked in your html file. You can do so either by downloading from api.jquery.com or by a CDN which you can find on the same site under the download section.

Olu Gbadebo
Olu Gbadebo
Courses Plus Student 516 Points

Problem still persists. I don't think it's jquery because jquery is not used in NodeJS

Here is my code: '''javascript

var Profile = require("./profile.js"); var renderer = require("./renderer.js");

//2. Handle HTTP route GET / and POST function home(request, response) { //if url == "/" && GET if (request.url == "/") { //show search response.writeHead(200, {'Content-Type': 'Text/plain'}); renderer.view("header", {}, response); response.end("Footer \n"); } } //if url == "/" && POST //redirect to /username

//3. HAndle HTTP route GET /:username function user(request, response){ //if url == "/...." var username = request.url.replace("/", ""); if (username.length > 0) { response.writeHead(200, {'Content-Type': 'Text/plain'});

//get json from Treehouse
var studentProfile = new Profile(username);

//on "end"
studentProfile.on("end", function(profileJSON){
    //show profile

  //store the values we need 

  var values = {
    avatarUrl : profileJSON.gavatar_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.write(error.message + "\n"); response.end("Footer \n"); }); } module.exports.home = home; module.exports.user = user; '''