Coder Social home page Coder Social logo

Comments (13)

Nikoms avatar Nikoms commented on May 17, 2024 6

Did you add import "reflect-metadata"; ?

from typedi.

NoNameProvided avatar NoNameProvided commented on May 17, 2024 1

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();
   }
}

from typedi.

pleerock avatar pleerock commented on May 17, 2024

Its strange that second is case is working it shall not work as well. Probably second case is a bug.

Your problem is that you mark service with a label, you need to call:

const accessTokenService = Container.get<AccessTokenService>('AccessTokenService'); // string here is a name of the service you provided in the decorator
accessTokenService.makeAccessToken(user);

instead of what you are doing right now. If you want to get service by a class then you need to remove service's label.

from typedi.

ibox4real avatar ibox4real commented on May 17, 2024

Thank you. As i see the type variable must be supplied when using get with string aliases.

from typedi.

ibox4real avatar ibox4real commented on May 17, 2024

I'm afraid this solution Container.get<AccessTokenService>('AccessTokenService') still does not inject the services into the constructor. I even removed the Require decorator and replaced it with Inject.

What am i missing here?

import {Container} from 'typedi';
import {AccessTokenService} from 'src/authentication/services/access-token-service';
import * as cfg from 'src/application/config';
import * as moment from 'moment';
import * as jsonwebtoken from 'jsonwebtoken';

Container.set('cfg.auth.jwt', cfg.auth.jwt);
Container.set('moment', moment)
Container.set('jsonwebtoken', jsonwebtoken);
const accessTokenService = Container.get<AccessTokenService>('AccessTokenService');

Again, this does not work

import {Service, Inject} from 'typedi';
//..

@Service('AccessTokenService')
export class AccessTokenService {

    public constructor(
        @Inject('moment') private moment,
        @Inject('jsonwebtoken') private jsonwebtoken,
        @Inject('cfg.auth.jwt') private jwt
    ) {
        console.log(moment); //undefined
        console.log(jsonwebtoken);  //undefined
        console.log(jwt);  //undefined
    }
   //..
}

but this does

import {Service, Inject} from 'typedi';
//..

@Service('AccessTokenService')
export class AccessTokenService {

      @Inject('moment') 
      private moment;

      @Inject('jsonwebtoken')
      private jsonwebtoken;

      @Inject('cfg.auth.jwt') 
      private jwt;

   //..
}

from typedi.

ibox4real avatar ibox4real commented on May 17, 2024

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

In your example you are using concrete class implementations. But sometimes i also want my dependencies to be interface binded. That would not work, because there is now way to know which implementation to load. That is also why i use the service name in the @Service @Inject decorators.

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.

I do this to make testing easier. If i want to mock out moment, it is a lot easier this way. In plain javascript i could also use rewire but this does not work well with a typescript setup.

from typedi.

NoNameProvided avatar NoNameProvided commented on May 17, 2024

I do this to make testing easier

That makes sense, but then cannot you do this?

constructor(
    private momment: moment = moment,
  ) { }

No need for dependency injections, you still can mock it.

from typedi.

ibox4real avatar ibox4real commented on May 17, 2024

I could also do it that way.

Actually for testing this also works for me, because i can set the dependcies manually in the container

@Service('AccessTokenService')
export class AccessTokenService {

      @Inject('moment') 
      private moment;

      @Inject('jsonwebtoken')
      private jsonwebtoken;

      @Inject('cfg.auth.jwt') 
      private jwt;

   //..
}

My only problem is why it works on the properties but not in the constructor. This seems like an inconsistency. It should either work with both or work with neither.

from typedi.

pleerock avatar pleerock commented on May 17, 2024

@ibox4real can you please provide a demo git repo with minimal reproduction of this problem? I added test (see commit above) and it does not reproduce your issue

from typedi.

pleerock avatar pleerock commented on May 17, 2024

ping @ibox4real

from typedi.

ibox4real avatar ibox4real commented on May 17, 2024

Yes, i will try to reproduce it in a simplified setup within the next few days.

from typedi.

pleerock avatar pleerock commented on May 17, 2024

yeah, probably he forgot to import reflect-metadata. Closing.

from typedi.

github-actions avatar github-actions commented on May 17, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

from typedi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.