我对 LifecycleHooks 和 EventSubscribers 的概念相当陌生。我正在尝试填充一个历史表,记录在任何其他表上所做的更改。我一直在尝试使用 EventSubscriber 来实现此目的:这个想法是,在 eventSubscriber 的 onFlush 事件触发器上,我从变更集中提取所有更改的值和字段,并创建历史表的对象并尝试保留它,但我看到以下错误:
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason: ValidationError: You cannot call em.flush() from inside lifecycle hook handlers
Run Code Online (Sandbox Code Playgroud)
我的订阅者看起来像这样:
import { HistoryLog } from 'src/entities/history-log.entity;
export class EntityChangeSubscriber implements EventSubscriber<AnyEntity> {
async afterCreate(args: EventArgs<AnyEntity>): Promise<void> {
console.log('===========1111 AfterCreateHook');
console.log(args.entity);
}
async afterUpdate(args: EventArgs<AnyEntity>): Promise<void> { …Run Code Online (Sandbox Code Playgroud)