I wrote a BeforeCreate hook in my sequelize model. when i hit create user route then it saying user.user_id can't be null and even before create hook function not executing. I have followed documentation of sequelize.They have mentioned same as I use.I wrote a BeforeCreate hook in my sequelize model. when i hit create user route then it saying user.user_id can't be null and even before create hook function not executing. I have followed documentation of sequelize.They have mentioned same as I use.
const sequelize = require("kvell-db-plugin-sequelize").dbInstance;
const Sequelize = require("kvell-db-plugin-sequelize").dbLib;
const shortid = require("shortid");
const User = sequelize.define(
"user",
{
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false
},
user_id: {
type: Sequelize.STRING,
allowNull: false,
primaryKey: true,
unique: true
},
user_fname: {
type: Sequelize.STRING,
allowNull: false
},
user_lname: {
type: Sequelize.STRING,
allowNull: false
},
user_fullName: {
type: Sequelize.VIRTUAL,
get() {
return `${this.user_fname} ${this.user_lname}`;
},
set(value) {
throw new Error("Do not try to set the `fullName` value!");
}
},
user_email: {
type: Sequelize.STRING,
allowNull: false,
primaryKey: true,
validate: {
isEmail: true
},
unique: {
args: true,
msg: "Email address already in use!"
}
},
user_credential: {
type: Sequelize.STRING,
allowNull: false
},
user_roles: {
type: Sequelize.ARRAY(Sequelize.STRING),
allowNull: false,
defaultValue: ["Admin"]
},
admin: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: true
},
user_img: {
type: Sequelize.STRING,
allowNull: true
}
},
{
timestamps: true
}
);
User.beforeCreate(async (user, options) => {
console.log("inside hooks");
let id = `user_${shortid.generate()}`;
user.user_id = id;
});
const toJSON = User.prototype.toJSON;
User.prototype.toJSON = function({ attributes = [] } = {}) {
const obj = toJSON.call(this);
if (!attributes.length) {
return obj;
}
return attributes.reduce((result, attribute) => {
result[attribute] = obj[attribute];
return result;
}, {});
};
module.exports = User;
The real answer (alluded to, but not explicitly stated, in one of the comments) is that beforeCreate hooks are applied after model validation.
This means if you have any field in your model (eg id ) which cannot be null, Sequelize will evaluate this prior to applying the beforeCreate field. In your case, Sequelize never gets as far as the beforeCreate hook, because the null id is failing validation every time.
Your accepted answer works around this by setting allowNull = true, thus circumventing the validation of your (briefly) null id. But the better option (rather than to distort your model by allowing null id) is almost certainly to instead use the correct hook: beforeValidate. This hook is applied before the model criteria are evaluated.
It is a very simple change:
User.beforeValidate(async (user, options) => {
console.log("inside hooks");
let id = `user_${shortid.generate()}`;
user.user_id = id;
});
NB: async is redundant here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With