majidarif
majidarif

Reputation: 20025

Bookshelf JS function inside extend()

I am using http://bookshelfjs.org/

I added a function inside

var User = Bookshelf.Model.extend({
...
// verify the password
verifyPassword: function (password, hash, done) {
  // Load hash from your password DB.
  bcrypt.compare(password, hash.replace('$2y$', '$2a$'), function(err, result) {
    return done(err, result);
  });
}
...
module.exports = User;

from my user controller, I call it like:

var User = require('../models/user');
User.verifyPassword(req.body.password, user.password, function (err, result) {

But I am getting no method 'verifyPassword.

Upvotes: 2

Views: 1901

Answers (1)

Scott Puleo
Scott Puleo

Reputation: 3684

You need to create an instance of User in your controller

var User = require('../models/user');
this.user = new User();

Of if you want your user module to always return an instance then modify it to something like this.

var User = Bookshelf.Model.extend({
   ...
}
...
module.exports = function(options){ 
  return new User(options) 
};

Upvotes: 2

Related Questions