NestJs-无法注入以在订阅服务器上工作 [英] NestJs - not getting Injectable to work on subscriber

查看:17
本文介绍了NestJs-无法注入以在订阅服务器上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用订阅者(基于类型ORM的数据库)找不到如何访问请求(让授权用户通过睡觉API创建预订)

我尝试使用doc中所述的Injectable,但似乎在这里不起作用。

感谢您的帮助

booking.scriber.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;
  }
}

推荐答案

如文档中所述,Event Subscribers cannot be request scoped。您尝试执行的操作在Nest中是不可行的,因为TypeORM和Nest具有不同的容器系统来保存元数据。

这篇关于NestJs-无法注入以在订阅服务器上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆