Carson
Carson

Reputation: 1257

Trying to include functions from other JavaScript files, TypeError that it's not a function

I'm trying to make my code cleaner in that seperating functions into other files. Namely, I have a UsersController.js that will perform functions on the Users database. With only one function inside right now...

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

module.exports = function(){
    this.verifyNoExistingUser = function verifyNoExistingUser(email, name){
        //check if email is taken
        User.findOne({email: email}, function(err, user){
            if(err){
                return res.status(500).send('Error on the server.');
            }
            if(!user){
                //check if username is taken
                User.findOne({name: name}, function(err, user){
                    if(err){
                        return res.status(500).send('Error on the server.');
                    }
                    if(!user){
                        return true;
                    }
                });
            }

            return false;
        });
    }
};

Then when I go to use it in my app.js, like such....

var express = require('express');
var router = express.Router();
...
var UsersController = require('../user/UsersController.js');
...
router.post('/register', function(req, res){
    var hashedPassword = bcrypt.hashSync(req.body.password, 8);

    if(!UsersController.verifyNoExistingUser(req.body.email, req.body.name)){
        console.log(val);
        return res.status(500).send("Username or email already exists.");
    }

I'm getting that my function is not a function. When I call...

UsersController.verifyNoExistingUser(req.body.email, req.body.name)

I was specifically trying to follow this SO question but not getting a correct result. Any help on how to include functions from other JS files?

Upvotes: 1

Views: 58

Answers (5)

user2590928
user2590928

Reputation: 176

try

module.exports = {
    verifyNoExistingUser: function (email, name){
        //check if email is taken
        User.findOne({email: email}, function(err, user){
            if(err){
                return res.status(500).send('Error on the server.');
            }
            if(!user){
                //check if username is taken
                User.findOne({name: name}, function(err, user){
                    if(err){
                        return res.status(500).send('Error on the server.');
                    }
                    if(!user){
                        return true;
                    }
                });
            }

            return false;
        });
    }
};

Upvotes: 0

Arif Khan
Arif Khan

Reputation: 5069

You have defined as function and calling as object, you can either call function as following

var UsersController = require('../user/UsersController.js')();

or define as object

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

module.exports = {
    this.verifyNoExistingUser = function verifyNoExistingUser(email, name){

    }
};

Upvotes: 1

Neeraj Wadhwa
Neeraj Wadhwa

Reputation: 737

You can do something like this:

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

function verifyNoExistingUser(email, name){
        //check if email is taken
        User.findOne({email: email}, function(err, user){
            if(err){
                return res.status(500).send('Error on the server.');
            }
            if(!user){
                //check if username is taken
                User.findOne({name: name}, function(err, user){
                    if(err){
                        return res.status(500).send('Error on the server.');
                    }
                    if(!user){
                        return true;
                    }
                });
            }

            return false;
        });
    }
  module.exports = {
  verifyNoExistingUser,
}

Upvotes: 0

Dong Nguyen
Dong Nguyen

Reputation: 1269

Why you don't simply write:

module.exports = (email, name){
    //check if email is taken
    User.findOne({email: email}, function(err, user){
        if(err){
            return res.status(500).send('Error on the server.');
        }
        if(!user){
            //check if username is taken
            User.findOne({name: name}, function(err, user){
                if(err){
                    return res.status(500).send('Error on the server.');
                }
                if(!user){
                    return true;
                }
            });
        }

        return false;
    });
};

Then in the another file:

var verifyNoExistingUser = require('../user/UsersController.js');

(you may rename UsersController.js to verifyNoExistingUser.js

And call it:

verifyNoExistingUser(req.body.email, req.body.name)

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 370679

In your top file, you're exporting a function:

module.exports = function(){

In your bottom file, you're importing the function:

var UsersController = require('../user/UsersController.js');

and trying to access one if its properties:

if(!UsersController.verifyNoExistingUser...

which of course doesn't exist. Assigning to a this inside a function doesn't assign to the function's properties itself, and even if it did, you would have to run the function first for the property to be assigned.

If you want to consume it like that, you should export an object instead:

var User = require('../user/User');
module.exports = {
  verifyNoExistingUser: function verifyNoExistingUser(email, name){
    //check if email is taken
    User.findOne({email: email}, function(err, user){
    // ...

But if verifyNoExistingUser is the only function you want to export, then why not export it directly, rather than export an object?

module.exports = function verifyNoExistingUser(email, name){

Upvotes: 2

Related Questions