问题
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
.
回答1:
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)
};
来源:https://stackoverflow.com/questions/22114865/bookshelf-js-function-inside-extend