1

I get the following error when I attempt to call the generateHash instance method I've defined on my User model:

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

Here's the model definition itself:

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
            };
        }
    }
});

Here's the endpoint where I attempt to call the method:

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'});
    });});

I'm totally stuck. Any ideas?

0

2 Answers 2

7

In sequelize V4 class and instance methods are removed. Now you have to make it this way:

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

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

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

More information here Sequelize v4 breaking changes

Sign up to request clarification or add additional context in comments.

Comments

1

You are calling .then() on something that does not return a promise. Try this:

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'});
    });

1 Comment

Thanks! I ended up implementing a similar solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.