NestJs - 无法让 Injectable 对订阅者起作用

Ant*_*bbs 5 nestjs

使用订阅者(基于 TypeORM 的数据库)我找不到如何访问请求(让授权用户通过 Rest api 创建预订)

我尝试按照文档中的描述使用 Injectable ,但似乎在这里不起作用。

感谢帮助

预订.subscriber.ts

import { Inject, Injectable, Scope } from '@nestjs/common';
import { throwError } from 'rxjs';
import { Account } from 'src/accounts';
import { Room } from 'src/rooms';
import { TransactionEntity, TransactionsModule } from 'src/transactions';
import {
  EventSubscriber,
  EntitySubscriberInterface,
  InsertEvent,
  getRepository,
  LessThan,
  MoreThan,
} from 'typeorm';
import { Booking } from './booking.entity';
import { REQUEST } from '@nestjs/core';
import { Request } from 'express';

@EventSubscriber()
@Injectable({ scope: Scope.REQUEST })
export class BookingsSubscriber implements EntitySubscriberInterface<Booking> {
  constructor(@Inject(REQUEST) private request: Request) {
    console.log('request', this.request) // return undefined
  }

  listenTo() {
    return Booking;
  }
}
Run Code Online (Sandbox Code Playgroud)

Jay*_*iel 4

如文档中所述,事件订阅者不能处于请求范围内。由于 TypeORM 和 Nest 有不同的容器系统来保存元数据,因此您尝试做的事情在 Nest 中是不可能的。