title: TypeError: User.createUser is not a function, at index.js 62:8
body: <p>i am making a simple node express app and i was making signup funcitonality. I am calling createUser from my index route and it calls the function in my User model and then the password is encrypted and stored. But when it calls the createUser function from index route, it gives the above error. I can't figure out what the problem is!!</p>
  
  <p>Here's my index route file</p>
  
  <pre><code>    var express = require('express');
      var router = express.Router();
      var User = require('../models/User.js');
  
  
      /* GET home page. */
      router.get('/', function(req, res, next) {
      res.render('index', { title: 'Express' });
  }); 
  
  
  router.post('/login', function(req, res, next){
  
      var username = req.body.username;
      var password = req.body.password;
  
      User.getUserByUsername(username, function(err, user){
              if(err) throw err;
  
              if(!user){
                  return res.send({msg: 'no user was found with this username'});
              }
  
              User.checkPassword(user, password, function(err, result){
                  if (err) {
                      throw err;
                      return null;
                  }
                  if(user){
                      res.send({msg:'You are now logged in'});
                  }else{
                      res.send({msg: 'Your password is wrong'});
                  }
  
              })
      });
  
  });
  
  router.post('/signup', function(req, res, next){
  var username = req.body.username;
  var password = req.body.password;
  var password2 = req.body.password2;
  console.log(req.body);
  
  req.checkBody('username', 'username field is required').notEmpty();
  req.checkBody('password', 'password field is required').notEmpty();
  req.checkBody('password2', 'confirm password field is required').notEmpty();
  
  var errors = req.validationErrors();
  
  console.log(errors);
  if(errors){
      req.flash('error', 'there was some error with the signup process');
      res.send('there was some validation error');
  }else{
      var newUser = new User({    
          username: username,
          password:password
      });
      console.log(newUser);
      User.createUser(newUser, function(err, user){
          if (err) throw err;
          console.log(user);
          res.send('user was created');
      })
  }
  
    });
  
  module.exports = router;
  </code></pre>
  
  <p>and here's my model and schema</p>
  
  <pre><code>        var mongoose = require('mongoose');
  mongoose.connect('mongodb://localhost/quizapp');
  var db = mongoose.connection;
  var bcrypt = require('bcrypt');
  
  var UserSchema = mongoose.Schema({
      username:{
          type:String,
          required: true
      },
      password:{
          type:String,
          required:true,
          bcrypt:true
      }
  });
  
  module.exports.getUserByUsername = function(username, callback){
      User.findOne({username:username}, callback);
  }
  module.exports.checkPassword = function(pass, callback){
      callback(null, true);
  }
  
  module.exports.createUser = function (newUser, callback) {
      bcrypt.hash(newUser.password, 10, function(err, hash){
          if(err) throw err;
          newUser.password = hash;
          User.save(newUser, callback);
      });
  
  };
  var User = module.exports = mongoose.model('User',UserSchema);
  </code></pre>