Lifecycle Events

There are two module lifecycle events OnModuleInit and OnModuleDestroy. You should use them for all the initialization stuff and avoid to work in the constructor. The constructor should only be used to initialize class members but nothing more. Example:

import { OnModuleInit, OnModuleDestroy } from '@nestjs/common';

@Component()
export class UsersService implements OnModuleInit, OnModuleDestroy {
    onModuleInit() {
        console.log('Module initialized...');    
    }
    onModuleDestroy() {
       console.log('Module destroyed...');
    }
}

results matching ""

    No results matching ""