2

https://github.com/stephenh/ts-proto/blob/main/NESTJS.markdown

From the ts-proto protject, in the document, I see the client is implemented with OnModuleInit like this

import { HeroById, Hero, HeroServiceController, HeroesService, HERO_SERVICE_NAME, HERO_PACKAGE_NAME } from '../hero';

@Injectable()
export class AppService implements OnModuleInit {
  private heroesService: HeroesService;

  constructor(@Inject(HERO_PACKAGE_NAME) private client: ClientGrpc) {}

  onModuleInit() {
    this.heroesService = this.client.getService<HeroesService>(HERO_SERVICE_NAME);
  }

  getHero(): Observable<Hero> {
    return this.heroesService.findOne({ id: 1 });
  }
}

I change code without OnModuleInit, and it works fine for me

import { HeroById, Hero, HeroServiceController, HeroesService, HERO_SERVICE_NAME, HERO_PACKAGE_NAME } from '../hero';

@Injectable()
export class AppService {
 
  constructor(@Inject(HERO_PACKAGE_NAME) private client: ClientGrpc) {}

  private heroesService: HeroesService = this.client.getService<HeroesService>(HERO_SERVICE_NAME)

  getHero(): Observable<Hero> {
    return this.heroesService.findOne({ id: 1 });
  }
}

So is there potential different between with or without OnModuleInit?

pifuegtq23
  • 171
  • 2
  • 10

0 Answers0