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 Handling Routes in Node.js Populating User Information

Carlos Villarroel
Carlos Villarroel
23,748 Points

TypeError Profile is not a constructor

All went well until I added the following line of code to the user():

var studentProfile = new Profile(username);

Afterwards whenever I run the program I get the following error message on the terminal:

TypeError: Profile is not a constructor

I'll figure this out eventually but any ideas or suggestion would be appreciated.

Carlos Villarroel
Carlos Villarroel
23,748 Points

OK so maybe I jumped to post the question too soon : )

I fixed the Profile function in profile.js to make it into a real constructor function and that solved the problem, used the following syntax in case anyone else runs into a similar problem:

let Profile = function(username) { ... }

Oh, and BTW I'm running node.js on the terminal on Windows 8.1.

Steven Parker
Steven Parker
231,172 Points

What did the original definition of Profile look like?

2 Answers

Carlos Villarroel
Carlos Villarroel
23,748 Points

Yes Steven, that did it, that was the only change I made and it solved the problem; the app worked perfectly from there. Don't know if you are taking the course but here is the original code:

function Profile(username) {...}

and you have my fix above.

Steven Parker
Steven Parker
231,172 Points

A named function should work equally well as a constructor. Could you show the whole code?

Steven Parker
Steven Parker
231,172 Points

I'm going to take a guess that the original definition of Profile used an "arrow function". Arrow functions are not exact replacements for conventional functions, and one important difference is that they do not bind "this". Constructors rely on "this" being a reference to the newly created object.

In the MDN page on arrow functions it says:

These function expressions are best suited for non-method functions, and they cannot be used as constructors.