title: Function is undefined, Bookshelf.js model function is not being recognized as a function
body: <p>I am using <a href="http://bookshelfjs.org/" rel="nofollow"><code>Bookshelf.js</code></a> to handle a user registration API end-point, designed with NodeJS and ExpressJS. But upon POSTing to the register url, I keep hitting an error at one of the User model functions.</p>
  
  <p>Here is routes/index.js</p>
  
  <pre><code>var User = require(./models/User);
  router.post('/register', function(req, res, next){
    if(!req.body.username || !req.body.password){
      return res.status(400).json({message: 'Please fill out all fields'});
    }
    try {
        var hash = User.createPassword(req.body.password);
        console.log(hash);
        new User({email: req.body.username, name: req.body.username, password: hash}).save().then(function(model) {
            return res.json({token: this.generateJWT()});
        });
    } catch (ex) {console.log(ex.stack);}
  });
  </code></pre>
  
  <p>Here is models/Users.js</p>
  
  <pre><code>var jwt = require('jsonwebtoken');
  var bcrypt = require('bcrypt');
  var bookshelf = require('../config/bookshelf');
  
  var User = bookshelf.Model.extend({
    tableName: 'users',
    constructor: function() {
      bookshelf.Model.apply(this, arguments);
      this.on('saving', function(model, attrs, options) {
        console.log(this.createPassword(model.attributes.password));
      });
    },
    createPassword: function(password) {
      bcrypt.genSalt(10, function (err, salt) {
        if(err) return next(err);
        bcrypt.hash(password, salt, function (err, hash) {
          if(err) return next(err);
          return hash;
        });
      });
    },
    validPassword: function(password, encryptedPass) {
      bcrypt.compare(password, user.encryptedPass, function (err, match) {
        if(err) cb(err);
        return (match) ? true : false;
      });
    },
    generateJWT: function() {
      // set expiration to 60 days
      var today = new Date();
      var exp = new Date(today);
      exp.setDate(today.getDate() + 60);
  
      return jwt.sign({
        _id: this._id,
        email: this.email,
        exp: parseInt(exp.getTime() / 1000),
      }, 'SECRET');
    }
  });
  
  module.exports = User;
  </code></pre>
  
  <p>When I try to POST to register, I get the following stack trace:</p>
  
  <pre><code>TypeError: undefined is not a function
      at \routes\index.js:185:21
      at Layer.handle [as handle_request] (\node_modules\express\lib\router\layer.js:95:5)
      at next (\node_modules\express\lib\router\route.js:131:13)
      at Route.dispatch (\node_modules\express\lib\router\route.js:112:3)
      at Layer.handle [as handle_request] (\node_modules\express\lib\router\layer.js:95:5)
  
  ...
  </code></pre>
  
  <p>In the stack trace, \routes\index.js:185:21 is the following line: <code>var hash = User.createPassword(req.body.password);</code> at <code>createPassword</code>.</p>
  
  <p>So what am I doing wrong here? Why is it failing to recognize the <code>createPassword</code> function?</p>