Bookshelf JS function inside extend()

女生的网名这么多〃 提交于 2019-12-12 18:29:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!