JohnsWort
JohnsWort

Reputation: 118

Nodejs TypeError: undefined is not a function

I'm following Treehouse Nodejs tutorial and ran into this error. Server response in router is unable to find the view method in renderer.

router.js:10
response.view("header", {}, response);
       ^
TypeError: undefined is not a function

renderer.js

var fs = require("fs");

function view(templateName, values, response) {
  //Read from the template files
  var fileContents = fs.readFileSync('./views/' + templateName +   '.html');
  //Insert values in to the content
  //Write out the contents to the response
  response.write(fileContents);
}

module.exports.view = view;

router.js

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

//Handle HTTP route GET / and POST / i.e. Home
function home(request, response) {
  //if url == "/" && GET
  if(request.url === "/") {
  //show search
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.view("header", {}, response);
  response.write("Search\n");
  response.end("Footer\n");
 }
}

Upvotes: 0

Views: 497

Answers (1)

Felix Kling
Felix Kling

Reputation: 816374

You are not using renderer anywhere in router.js.

I believe you meant to call renderer.view instead of response.view.

Upvotes: 1

Related Questions