问题描述
我正在使用 Bookshelf.js
来处理由NodeJS和ExpressJS设计的用户注册API端点.但是在发布到注册网址后,我一直在用户模型功能之一中遇到错误.
I am using Bookshelf.js
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.
这是routes/index.js
Here is routes/index.js
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);}
});
这是模型/Users.js
Here is models/Users.js
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;
当我尝试POST进行注册时,我得到以下堆栈跟踪:
When I try to POST to register, I get the following stack trace:
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)
...
在堆栈跟踪中,\ routes \ index.js:185:21是以下行:var hash = User.createPassword(req.body.password);
at createPassword
.
In the stack trace, \routes\index.js:185:21 is the following line: var hash = User.createPassword(req.body.password);
at createPassword
.
那么我在这里做错了什么?为什么无法识别createPassword
功能?
So what am I doing wrong here? Why is it failing to recognize the createPassword
function?
推荐答案
createPassword()
未定义为静态方法(也称为classProperties
),但您是这样称呼它的.尝试此模型定义.它应该将createPassword()
直接作为静态方法公开.
createPassword()
is not defined as a static method (a.k.a. classProperties
), but you are calling it as such. Try this model definition. It should expose createPassword()
directly on the User class as a static method.
var User = bookshelf.Model.extend({ //instance methods
tableName: 'users',
constructor: function() {
bookshelf.Model.apply(this, arguments);
// ...
},
validPassword: function(password, encryptedPass) {
// ...
},
generateJWT: function() {
// ...
}
}, { //static methods
createPassword: function(password) {
// ...
}
});
其他:您需要修复createPassword,因为它是异步的.下面,我将其转换为Promise-returning函数(由于书架广泛使用了promises),并显示了路由处理程序的示例用法
Extra: You'll need to fix your createPassword, as it's async. Below I've converted it to a Promise-returning function (as bookshelf uses promises extensively) and show an example usage for your route handler
createPassword: function () {
return new Promise(function (resolve, reject) {
bcrypt.genSalt(10, function (err, salt) {
if (err) return reject(err);
bcrypt.hash(password, salt, function (err, hash) {
if (err) return reject(err);
resolve(hash);
});
});
});
}
// in route handler
if (!req.body.username || !req.body.password) {
return res.status(400).json({
message: 'Please fill out all fields'
});
}
try {
User.createPassword(req.body.password)
.then(function(hash) {
console.log(hash);
return new User({
email: req.body.username,
name: req.body.username,
password: hash
}).save();
}).then(function (model) {
return res.json({
token: this.generateJWT()
});
}).catch(function(ex) {
console.log(ex.stack);
});
} catch (ex) {
console.log(ex.stack);
}
这篇关于函数未定义,Bookshelf.js模型函数未被识别为函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!