我试图在Sequelize模型中定义一个customFunction 。但是我得到一个错误:
TypeError:user.getJWT不是User.create.then的函数(/projects/test/a/app/controllers/UserController.js:22:29)
这是以下代码models/user.js:
module.exports = function(sequelize, Sequelize) {
var User = sequelize.define('User', {
id: {
type: Sequelize.INTEGER(11),
allowNull: true,
primaryKey: true,
autoIncrement: true
},
user_id: {
type: Sequelize.STRING(255),
allowNull: true,
defaultValue: ''
}
});
User.prototype.getJWT = function() {
let expiration_time = parseInt(CONFIG.jwt_expiration);
return "Bearer " + jwt.sign({
user_id: this.user_id,
role: this.role
}, CONFIG.jwt_encryption, {
expiresIn: expiration_time
});
}
return User
}
Run Code Online (Sandbox Code Playgroud)
这就是我在控制器中调用此原型函数的方式...
User.create(body).then((user) => {
user.token = user.getJWT(); // and here I get the error …Run Code Online (Sandbox Code Playgroud)