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 Binding Values

TypeError: undefined is not a function

Hi, I think I followed everything as Andrew did, but for some reason I am getting this error whenever I open the preview. The first page loads, then the error happens.

The error is: TypeError: undefined is not a function points to this: content = content.replace("{{" + key + "}}", values[key]);

Looks like it does not recognize the .replace method.

Here is my code:

var fs = require("fs");

function mergeValues(values, content) {
  // cycle over the keys 
    for (var key in values) {
      // replace all {{key}} with the values object
      content = content.replace("{{" + key + "}}", values[key]);
    }

  //return merged content
  return content;
}
// Function that handles the reading of files to the templates that we have and merge in values
  // read from file and get a string 
    // merge values in to string

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

module.exports.view = view;

I shared a workspace here, as it might be somewhere else. https://w.trhou.se/ylkhd1909r

does anybody know what is going on? Thank you! :)

rydavim
rydavim
18,785 Points

I haven't completed the Node.js course yet, but it sounds like perhaps content is not a string?

4 Answers

josue exhume
josue exhume
20,981 Points

you guys are right, just add the tooString method:

content = content.toString().replace("{{"+ key +"}}", values[key]);

hi rydavim

I just tested it to be sure, but did not work. I believe the function parameters are set inside the function. the replace method only works against strings, so it should be a string anyways. But again, I might have no idea what I am talking about, so don't take me too serious :)

Thank you either way!

Thank you josue exhume

That don't break the server, but the outcome is "[object Object][object Object][object Object]"

With the original code, the server actually loads the first page with the right code, then it breaks. Beats me!

josue exhume
josue exhume
20,981 Points

hmm thats weird. lol want me to post my js files for that course?