我正在尝试创建一个在为记录创建主键后应该调用的挂钩。我试图使用插入的列的组合生成一个数字,包括主键。实例挂钩似乎被调用,而全局挂钩没有被调用,即使我看到挂钩被调用,该字段仍然会返回null:
module.exports = function(sequelize, DataTypes) {
var store = sequelize.define('store', {
storenumber: DataTypes.STRING(30), //remove
storespecificationid: DataTypes.INTEGER,
storetypeid: DataTypes.INTEGER,
storename: DataTypes.STRING(20), //remove
address: DataTypes.STRING(30),
city: DataTypes.STRING(30),
state: DataTypes.STRING(30),
zipcode: DataTypes.STRING(30)
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
},
hooks: {
/*this is not getting called*/
afterCreate: function(store){
let strnumber = store.storetypeid + store.storespecificationid + store.id;
store.updateAttributes({ storenumber: strnumber });
}
}
}
});
/*
This seems to be called
store.afterCreate(function(store) {
let strnumber = store.storetypeid + store.storespecificationid + store.id;
store.updateAttributes({ storenumber: strnumber });
});
*/
return store;
};
有人能否解释我做错了什么?
答案 0 :(得分:1)
我认为你有一个额外的对象定义了classMethods
尝试下面的架构
module.exports = function(sequelize, DataTypes) {
var store = sequelize.define('store', {
storenumber: DataTypes.STRING(30), //remove
storespecificationid: DataTypes.INTEGER,
storetypeid: DataTypes.INTEGER,
storename: DataTypes.STRING(20), //remove
address: DataTypes.STRING(30),
city: DataTypes.STRING(30),
state: DataTypes.STRING(30),
zipcode: DataTypes.STRING(30)
}, {
associate: function(models) {
// associations can be defined here
},
hooks: {
/*this is not getting called*/
afterCreate: function(store){
let strnumber = store.storetypeid + store.storespecificationid + store.id;
store.updateAttributes({ storenumber: strnumber });
}
}
});