无法访问Sequelize实例方法

时间:2017-05-13 14:09:16

标签: node.js postgresql sequelize.js

当我尝试调用我在用户模型上定义的generateHash实例方法时出现以下错误:

User.generateHash(...).then is not a function

这里是模型定义本身:

const User = sequelize.define('User', 
{
    firstName: {
        type: Sequelize.TEXT,
        field: 'first_name'
    },
    lastName: {
        type: Sequelize.TEXT,
        allowNull: false,
        field: 'last_name'
    },
    userName: {
        type: Sequelize.TEXT,
        field: 'user_name',
        allowNull: false
    },
    password: {
        type: Sequelize.TEXT,
        allowNull: false
    }
}, {
    tableName: 'users',
    underscored: true,
    classMethods: {
        associate: function(models) {
            User.hasMany(
      models.Trip,
                {
                    as: 'trips',
                    foreignKey: {
                        name: 'userId',
                        field: 'user_id',
                        allowNull: false
                    },
                    onDelete: 'CASCADE'
                });
        },
    },
    instanceMethods: {
        generateHash: function(password) {
            return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
        },
        validatePassword: function(password) {
            return bcrypt.compareSync(password, this.password);
        },
        apiRepr: function() {
            return {
                id: this.id,
                firstName: this.firstName,
                lastName: this.lastName,
                userName: this.userName
            };
        }
    }
});

这是我尝试调用该方法的端点:

router.post('/', (req, res) => {
let {userName, password, firstName, lastName} = req.body;

// if no existing user, hash password
return User.generateHash(password)
    .then(hash => {
        // create new user
        return User.create({
            firstName: firstName,
            lastName: lastName,
            userName: userName,
            password: hash
        });
    })
    .then(user => {
        // send back apirRepr data
        return res.status(201).json(user.apiRepr());
    })
    // error handling
    .catch(err => {
        if (err.name === 'AuthenticationError') {
            return res.status(422).json({message: err.message});
        }
        res.status(500).json({message: 'Internal server error'});
    });});

我完全卡住了。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

在sequelize中,V4类和实例方法被删除。 现在你必须这样做:



const Model = sequelize.define('Model', {
    ...
});

// Class Method
Model.associate = function (models) {
    ...associate the models
};

// Instance Method
Model.prototype.someMethod = function () {..}




此处提供更多信息Sequelize v4 breaking changes

答案 1 :(得分:1)

您正在对未返回承诺的内容调用.then()。试试这个:

router.post('/', (req, res) => {
let {userName, password, firstName, lastName} = req.body;
let hash = User.generateHash(password);
// if no existing user, hash password
return User.create({
        firstName: firstName,
        lastName: lastName,
        userName: userName,
        password: hash
    })
    .then(user => {
        // send back apirRepr data
        return res.status(201).json(user.apiRepr());
    })
    // error handling
    .catch(err => {
        if (err.name === 'AuthenticationError') {
           return res.status(422).json({message: err.message});
        }
        return res.status(500).json({message: 'Internal server error'});
    });