2

I'm trying to use a class that I created called user.js. The code for this user is:

function User(){};

User.prototype.addUser = function(){
    //Do stuff
    return 0;
};

module.exports = User;

I'm including it in my index.js route file, which looks like this:

var config = require('../lib/config');
var db = require('../lib/db');
var User = require('../lib/user');
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index', { title: 'Express' });
});

/* GET create user. */
router.get('/newuser', function(req, res, next) {
    *****var newuser = User.addUser();*****

    res.render('index', {   
                        user: newuser
                    });
});

module.exports = router;

However, when I visit localhost/newuser, I get the following error: TypeError: undefined is not a function. This error is being thrown in index.js on the line I marked with 5 asterisks above.

2 Answers 2

7

You are defining a constructor named User() and exporting it. But, then when you require('../lib/user'), you get the constructor function, but you never construct a new object with it so you don't actually have an object of type User, you just have the constructor and thus there is no method addUser() on the constructor.

Instead of this:

var User = require('../lib/user');

you can call the constructor function like this:

var u = require('../lib/user');
var User = new u();

Or, if you never need to make another one, you can do it all in one line:

var User = new (require('../lib/user'))();
Sign up to request clarification or add additional context in comments.

3 Comments

So is there a way to include both the constructor and other functions as well? Like, I can call var newuser = new User(); and also have the function User.addUser(); seperately?
@terpak - it depends upon what you're really trying to do. When you put addUser on the prototype, you made it available only on an instance of the User object, not on the constructor function. A constructor function is an object that can have properties also so in your module you could define User.addUser = function() {} and this would make a static function on your constructor. It's static because it is not a method of an instance, but rather just a ultility function that can be called at any time and does not operate on a specific User instance.
@jfriend00 I had to use var User = new (require('../lib/user'))(); Note the ( ... ) around the require statement. Otherwise, User is undefined. Using node version v4.2.4. Thank you for your answer btw.
0

Another way would be to change your User.js class to return something on these lines...

function User(){};

User.prototype.addUser = function(){
    //Do stuff
    return 0;
};

module.exports = new User();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.