Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] @Constant/@Value are not available inside the constructor #2407

Closed
1 task
ktalebian opened this issue Aug 10, 2023 · 2 comments
Closed
1 task
Assignees

Comments

@ktalebian
Copy link
Contributor

Information

  • Version: 7.34.2
  • Packages:

When using @Constant or @Value to inject the configuration into a client/service, the value is undefined inside the constructor.

Example

For example, I have

@Service()
export class GoogleClient {
  @Constant('sso.google')
  config: GoogleSSOConfig;

  constructor() {
    console.log(this.config);
  }

  doSomething() {
    console.log(this.config);
  }

In the example above, when the app boots, the constructor prints undefined but when I call doSomething() the config value exists. How come?

Acceptance criteria

  • Criteria 1
@Romakita
Copy link
Collaborator

Hello @ktalebian

It's an intended behavior. Decorators used on properties like Constant/Value/Inject are filed by the injector service after the construtor. All props are filled before the $onInit hook.

If you need settings on constructor just inject the Configuration service:

construtor(settings: Configuration){
  const value = settings.get<string>('myValue');
}

Is equivalent of:

export class MyService {
  @Constant('myValue')
  value: string;

  $onInit(){
    console.log(this.value)
  }

See you
Romain

@github-actions
Copy link

🎉 Are you happy?

If you appreciated the support, know that it is free and is carried out on personal time ;)

A support, even a little bit makes a difference for me and continues to bring you answers!

github opencollective

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants