-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
beforeUpdate hook is not being called #4190
Comments
Duplicate of #4170 (you are passing the instance and class methods in two separate objects, they should be in the same) |
thank you. i forgot to save my updated version of the question. can you please help me as to why beforeUpdate isn't being called but beforeCreate is? |
You are calling bulk update (which updates several), so you should attach a |
AHAAA. thank you for the help! 😃 |
Hello. I have a question: is it possible to activate the db.models.customers.update(args, { where: {id: args.id}, individualHooks: true }) Is there any other way than passing that option at runtime? |
@DoctypeRosenthal You can add a |
Thanks for your quick reply! |
|
Thanks! I found the order in which all hooks are called:
Can I set the |
Yes, the option applies to both |
Thx man! Maybe someone could point this out more clearly in the documentation... Great work otherwise! I really appreciate Sequelize! |
Has this change? my beforeUpdate hook is not running const Moment = require('moment');
var Sequelize = require('sequelize');
var Connection = require('../database/Connection');
var bcrypt = require('bcrypt');
var sequelize = Connection.getInstance();
var User = sequelize.define('User', {
username: { type: Sequelize.STRING, unique: true },
name: Sequelize.STRING,
password: Sequelize.STRING,
token: Sequelize.STRING,
profile: Sequelize.STRING(4000)
}, {
timestamps: true,
tableName: 'User',
hooks: {
beforeCreate: (user, options, cb) => {
bcrypt.hash(user.password, 10, (err, hash) => {
user.password = hash;
return cb(null, options);
});
},
beforeUpdate: (user, options, cb) => {
bcrypt.hash(user.password, 10, (err, hash) => {
user.password = hash;
return cb(null, options);
});
}
},
instanceMethods: {
comparePassword: function compare (candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.getDataValue('password'), (err, isMatch) => {
if (err) {
cb(err);
} else {
cb(null, isMatch);
}
});
}
},
getterMethods: {
createdAt: function getCreadtedAt () {
return Moment(this.getDataValue('createdAt')).format('DD/MM/YYYY HH:mm:ss');
}
}
});
module.exports = User; im running return new Promise((resolve, reject) => {
sequelize.models.User.update({
password: 1234
}, {
individualHooks: true,
where: {
id: 1
}
})
.then(resolve)
.catch(reject);
}); |
If I run something like: return new Promise((resolve, reject) => {
sequelize.models.User.update({
name: 'ADMIN'
}, {
individualHooks: true,
where: {
id: 1
}
})
.then(resolve)
.catch(reject);
}); Then the beforeUpdate hook runs and hashes the password, but NOT on the password update itself |
NEVERMIND! for some reason it's working now ..... nargles |
Can you help my case ? |
My problem was that the Hook only affects individual Instances for ex:
when updating from the model, for ex: the hook doesnt run because it's an individual hook, you need to set the option individualHooks
or update the User from the user Instance rather than the Model |
@janmeier when the |
@jedwards1211 Oh my god yes. That would be great! Why is so much of sequeilize so un-intuitive? And why does documentation not tell us about any such quirks? I have to go treasure hunting for clues on Google/SO/Github to do anything with this library. |
@AdityaAnand1I agree! Sequelize is relatively unintuitive in comparison. Wish there was an orm like SQLAlchemy for node... |
Hi guys from 2018 ! Thank you for this topic, it helped me a lot a year later ! |
This "individualHooks: true" defenitely should be in a documentation with simple example, i wasted about an hour to find the problem |
user.model.js:
On Sequelize update call: updateUserById.js:
models is from Sequelize models defined |
Those docs for this library is an hot mess... |
Thanks @janmeier, saved my day. |
Thank you this just saved me after like an hour, I even tried going through the instance way. By finding one from the DB |
await Member.update(data, {where: {id: id}, individualHooks: true}); Adding |
Hello,
I am trying to call the beforeUpdate hook which uses the same function as beforeCreate hook but it doesn't even get called:
And i try to hash the password before update but the hook doesnt even get called:
Any idea why the password is hashing beforeCreate but not beforeUpdate? What am i doing wrong?
The text was updated successfully, but these errors were encountered: