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?