1

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?

Carson
  • 1,147
  • 5
  • 19
  • 41

5 Answers5

2

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){
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

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){

    }
};
Arif Khan
  • 5,039
  • 2
  • 16
  • 27
0

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)
Dong Nguyen
  • 1,239
  • 9
  • 17
0

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,
}
Neeraj Wadhwa
  • 645
  • 2
  • 7
  • 18
0

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;
        });
    }
};
user2590928
  • 176
  • 10