[英]How to add hooks in sequelize Nest.JS
throw new model_not_initialized_error_1.ModelNotInitializedError(this, Member "${key}" cannot be called.
); ^ 錯誤:模型未初始化:無法調用成員“afterCreate”。 “用戶”需要添加到 Sequelize 實例。
@Table({
tableName: 'users',
})
export class User extends Model<User> {
@Column({
type: DataType.STRING,
allowNull: false,
})
first_name: string;
....
}
User.afterCreate(async (user, options) => {
console.log('New User created:');
console.log(user.first_name);
console.log(user.email);
});
正如我所說的那樣,您正在使用sequelize-typescript
。 如果是,那么您需要使用另一種方法來定義掛鈎。
import { AfterCreate, Table, Model } from 'sequelize-typescript';
@Table
export default class User extends Model {
// ... definition of your columns and other model-related stuff
@AfterCreate
static methodName(instance: User) {
// do something
}
}
您可以在sequelize-typescript
的npm 包頁面上閱讀更多內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.