lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.48k stars 498 forks source link

Variables in service class instance are getting wiped out on each request to the controller. #1648

Closed vedtam closed 2 months ago

vedtam commented 3 months ago

Hi Guys,

Despite me reusing the same service instance, variables set within the service instance are gone whenever I try getting getCrawlStatus. Does that mean that each request uses a new instance of the controller?

@Route('crawler')
export class CrawlerController extends Controller {
  private crawlerService: CrawlerService;

  constructor() {
    super();
    this.crawlerService = new CrawlerService();
  }

  @Post('crawl')
  public async crawl(@Body() body: { siteId: number }): Promise<any> {
    return this.crawlerService.crawl(body.siteId);
  }

  @Get('status/{siteId}')
  public async getCrawlStatus(@Path() siteId: number): Promise<any> {
    return this.crawlerService.getCrawlStatus(siteId);
  }
}

If I create a global variable in the service, above the class, the data is persisted.

Thanks!

github-actions[bot] commented 3 months ago

Hello there vedtam 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

WoH commented 2 months ago

Does that mean that each request uses a new instance of the controller?

Yes. If you want to hook into this, you can provide your own iocModule, then you can return the same class instance every time.