question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Constructor inject not working

See original GitHub issue

Hei, i’m experiencing weird behaviour in my tests with constructor injects

//..
import {Container} from 'typedi';
import {AccessTokenService} from 'src/authentication/services/access-token-service';
import * as cfg from 'src/application/config';
Container.set('cfg.auth.jwt',cfg.auth.jwt);
const accessTokenService = Container.get(AccessTokenService);
accessTokenService.makeAccessToken(user);
//..

Dependecies are undefined when i configure them in the constructor


import {Service, Inject, Require} from 'typedi';
// ..
@Service('AccessTokenService')
export class AccessTokenService {

    constructor(
        @Require('moment') private moment,
        @Require('jsonwebtoken') private jsonwebtoken,
        @Inject('cfg.auth.jwt') private jwt
    ) {}

    makeAccessToken(user: User) {
        console.log(typeof this.jsonwebtoken); //undefined
        console.log(typeof this.jwt); //undefined
        console.log(typeof this.moment); //undefined
        // ...
    }
}

But this works


import {Service, Inject, Require} from 'typedi';
// ..
@Service('AccessTokenService')
export class AccessTokenService {

    @Require('moment') private moment;
    @Require('jsonwebtoken') private jsonwebtoken;
    @Inject('cfg.auth.jwt') private jwt;
  
    makeAccessToken(user: User) {
        console.log(typeof this.jsonwebtoken); //object
        console.log(typeof this.jwt); //object
        console.log(typeof this.moment); //function
        // ...
    }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
Nikomscommented, Nov 27, 2017

Did you add import “reflect-metadata”; ?

1reaction
NoNameProvidedcommented, Nov 16, 2017

When using classes, you don’t need to use @Inject when injecting in the constructor. You simply write:

constructor(
    private userService: UserService,
    private loyaltyService: LoyaltyService,
    private timerService: TimerService,
  ) { }

I don’t know about using them with default exports for other modules, but why do you want to inject them at all? You can just start using them right away.

import * as moment from 'moment';
// ....

@Service('AccessTokenService')
export class AccessTokenService {
   //...
   public myMethod() {
      return moment().format();
   }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found