title: Creating a instance using a model shows 'not a function' (node using mongoose)
body: <p>II make a user model and want to use it in the users.js route file.but it shows me <strong>userModel is not a function</strong>. anyone know what's happening here? Thanks</p>
  
  <p>//my User model in models/user.js</p>
  
  <pre><code>var mongoose = require('mongoose');
  var bcrypt = require('bcrypt-nodejs');
  
  var url = 'mongodb://fr:fran98@ds013250.mlab.com:13250/node';
  mongoose.connect(url); 
  
  var db = mongoose.connection;
  
  
  db.on('error', console.error.bind(console, 'connection error:'));
  
  db.once('open', function() {//when connected
          console.log('connected to mongo db');
          //User Schema
          var UserSchema = mongoose.Schema({
              username: {
                  type:String,
                  index:true
              },
              email: {
                  type:String
              },
              password: {
                  type:String,
                  required:true,
                  bcrypt:true
  
              },
              profileimage: {
                  type:String
              }
          }, { timestamps: 'created_at' } );   
  
          //add "speak" functionality to our documents:  
          UserSchema.methods.speak = function(){
              var greeting = this.name
                  ? "His name is " + this.name
                  : "I do not have a nanme";
              console.log(greeting);
          }       
  
          module.exports = {
              userModel: mongoose.model('User',UserSchema),
              createUser: function(newUser,callback){
                  bcrypt.hash(newUser.password, null, null, function(err, hash) {
                      // Store hash in your password DB.
                      newUser.password = hash;
                      newUser.save(callback);
  
                  });
  
              }
          };               
  });
  </code></pre>
  
  <p>//routes/users.js</p>
  
  <pre><code>    const User = require('../modules/user');
  
      var userModel = User.userModel;
      var newUser = new userModel({
          username:username,
          email:email,
          password:password,
          profileimage: profileImageName
      });
  
      //Create User
      User.createUser(newUser,function(err,user){
          if(err){throw err}
          console.log(user);
      });
  </code></pre>
  
  <p><strong>ADDS :</strong>
  //more about routes/users.js</p>
  
  <pre><code>"use strict";
  const express = require('express');
  const router = express.Router();
  
  const multer = require('multer');
  
  const upload = multer({ dest: '/uploads' });
  
  const User = require('../modules/user');
  //this User returns: 1,User = mongoose.model('User',UserSchema)
  //2: User.createUser = function(newUser,callback){
          //  newUser.save(callback);
          // }    
  
  /* GET users listing. */
  router.get('/', function(req, res, next) {
    res.send('respond with a resource');
  });
  
  router.get('/register', function(req, res, next) {
    res.render('register',{
        'title': 'Register',
        'errors':[]
    });
  });
  router.post('/register',upload.single('profileimage'), function(req, res, next) {
      var username = req.body.username;
      var email = req.body.email;
      var password = req.body.password;
      var password2 = req.body.password2;
  
      console.log(username,email,password,password2);
      //check for image field
      if(req.file){
          console.log('uploading file...');
  
          //file info
          var profileImageOriginalName = req.file.originalname;//the name before uploaded
          var profileImageName = req.file.filename;//name the server give it when it uploaded
  
          var profileImageMime = req.file.mimetype;
          var profileImagePath = req.file.path;
          var profileImageSize = req.file.size;
      }else{
          //set a default image
          profileImageName = 'noimage.png'; 
      }
      //form validation, using express-validator
  req.checkBody({
   'email': {
      notEmpty: true,
      isEmail: {
        errorMessage: 'Invalid Email'
      }
    },
    'password': {
      notEmpty: true,
      isLength: {
        options: [{ min: 4, max:20 }],
        errorMessage: 'Password Must be between 4 and 8 chars long' // Error message for the validator, takes precedent over parameter message
      },
      errorMessage: 'Invalid Password' // Error message for the parameter
    },
    'password2': {
      notEmpty: true,
      matches: {
        options: [password, 'g'] // pass options to the validator with the options property as an array
        // options: [/example/i] // matches also accepts the full expression in the first parameter
      },
      errorMessage: 'Password Not Match' // Error message for the parameter
    },
    'username': { //
      //optional: true, // won't validate if field is empty
      notEmpty: true,
      isLength: {
        options: [{ min: 4, max: 50 }],
        errorMessage: 'Username Must be between 4 and 8 chars long' // Error message for the validator, takes precedent over parameter message
      },    
      errorMessage: 'Invalid Name'
    }
  });
  
  
  
      // req.checkBody('username','userName field is required').notEmpty();
  
      // req.checkBody('email','Email field not valid').isEmail();
      // req.checkBody('password','Password field is required').notEmpty();
      // req.checkBody('password2','Password do not match').equals(req.body.password);
  
      //check for errors
      var errors = req.validationErrors();
      //or let errors = req.asyncValidationErrors();
      //errors is like below:
      //[
      //   {param: "email", msg: "required", value: "&lt;received input&gt;"},
      //   {param: "email", msg: "valid email required", value: "&lt;received input&gt;"},
      //   {param: "password", msg: "6 to 20 characters required", value: "&lt;received input&gt;"}
      // ]
  
      if(errors){
          res.render('register',{
              'title': 'Register',
              'errors': errors,
              'username':username,
              'email':email,
              'password':password,
              'password2':password2
          });
      }else{
          console.log('pass the validation.');
          var userModel = User.userModel;
          // var newUser = new userModel({
          //     username:username,
          //     email:email,
          //     password:password,
          //     profileimage: profileImageName
          // });
  
  userModel.create({ 
              username:username,
              email:email,
              password:password,
              profileimage: profileImageName
  }, function (err, res) {
    if (err) return handleError(err);
    console.log(res);
    // saved!
  })
  
  
          //Create User
          User.createUser(newUser,function(err,user){
              if(err){throw err}
              console.log(user);
          });//user model
          // newUser.save(function(err,newUser){
          //     if(err){console.log(err);}
          //     console.log(newUser);
          // });
  
          //success Message
          req.flash('success','you are now registered and may log in');
          //redirect to the home page
          res.location('/');
          res.redirect('/');
      }
  });
  
  
  
  
  router.get('/login', function(req, res, next) {
    res.render('login',{
        'title': 'Login'
    });
  });
  module.exports = router;
  </code></pre>