相关疑难解决方法(0)

排序Model.prototype。(customFunction)不起作用

我试图在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)

javascript node.js express sequelize.js

1
推荐指数
1
解决办法
2212
查看次数

标签 统计

express ×1

javascript ×1

node.js ×1

sequelize.js ×1