Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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; '''