Skip to content

Constructor inject not working  #40

@ibox4real

Description

@ibox4real

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
        // ...
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: awaiting answerAwaiting answer from the author of issue or PR.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions