title: How to fix "TypeError: cb is not a function" error for comparing passwords
body: <p>I've been starting to add user authentication into my app and when adding the login route, i've been getting the error "TypeError: cb is not a function". I know it is coming from my login route as all my other routes work fine. </p>
  
  <p>I have tried researching the issue and trying a few fixes that i've found but none have worked. So i'm starting to believe i've messed up somewhere and I can't find where.</p>
  
  <p>Login Route: </p>
  
  <pre><code>router.post('/login', function (req, res) {
      User.findOne({ username: req.body.username }, function (err, user) {
          if (err || user == null) {
              console.log(err);
              res.redirect('/login');
          }
  
          if (!user.comparePassword(req.body.password)) {
              req.flash('invalidDetails', 'Wrong username or password!');
              res.redirect('/login');
          } else {
              req.session.userId = user._id;
              req.flash('loggedIn', 'User ' + req.body.username + ' has been logged in!');
              return res.redirect('/');
          }
      });
  });
  </code></pre>
  
  <p>User Model:</p>
  
  <pre><code>var mongoose = require('mongoose'),
      Schema = mongoose.Schema,
      bcrypt = require('bcrypt'),
      SALT_WORK_FACTOR = 10;
  
  var UserSchema = new mongoose.Schema({
      email: {
          type: String,
          unique: true,
          required: true,
          trim: true
      },
      username: {
          type: String,
          unique: true,
          required: true,
          trim: true
      },
      password: {
          type: String,
          required: true
      }
  });
  
  UserSchema.statics.authenticate = function (email, password, callback) {
      User.findOne({ email: email }).exec(function (err, user) {
          if (err) {
              return callback(err);
          } else if (!user) {
              var err = new Error('User not found.');
              err.status = 401;
              return callback(err);
          }
          bcrypt.compare(password, hash, function (err, result) {
              if (result === true) {
                  return callback(null, user);
              } else {
                  return callback();
              }
          });
      });
  };
  
  UserSchema.pre('save', function (next) {
      var user = this;
  
      // only hash the password if it has been modified (or is new)
      if (!user.isModified('password')) return next();
  
      // generate a salt
      bcrypt.genSalt(SALT_WORK_FACTOR, function (err, salt) {
          if (err) return next(err);
  
          // hash the password along with our new salt
          bcrypt.hash(user.password, salt, function (err, hash) {
              if (err) return next(err);
  
              // override the cleartext password with the hashed one
              user.password = hash;
              next();
          });
      });
  });
  
  UserSchema.methods.comparePassword = function comparePassword(candidatePassword, cb) {
      bcrypt.compare(candidatePassword, this.password, function (err, isMatch) {
          if (err) {
              return cb(err);
          }
  
          cb(null, isMatch);
      });
  };
  
  var User = mongoose.model('User', UserSchema);
  module.exports = User;
  </code></pre>
  
  <p>I'm expecting it to compare the password with the password that has been entered into the login form with the password that is stored in the database and log in the user if the password is correct or redirect back to the login page with the invalidDetails flash message if the password is incorrect.</p>
  
  <p>But what i'm actually getting is the "TypeError: cb is not a function" error when trying to login.</p>