Mikro EntityManager in NestJS bull processor not working

287 Views Asked by At

I want to make some queries in the task processor:

@Processor('audio')
export class AudioProcessor {

  constructor(private readonly entityManager: EntityManager) {

  }

  @Process()
  public async process(job: Job<any>) {
    // ! this promise never resolve
    const user = await this.entityManager.findOne(User, { id: 1 });
  }
}

The promise will never be resolved in the @Process() function.

Thanks.

1

There are 1 best solutions below

0
鸿则_ On BEST ANSWER

In fact, there is an error thrown here

@Process()
public async process(job: Job<any>) {
  // error thrown below
  const user = await this.entityManager.findOne(User, { id: 1 });
}

The correct solution is use the @UseRequestContext() decorator and orm property in constructor.


constructor(
    private readonly orm: MikroORM) {

}

@Process()
@UseRequestContext()
public async process(job: Job<any>) {
  // error thrown below
  const user = await this.entityManager.findOne(User, { id: 1 });
}