Tsed: Dependency injection failing on standalone service

Created on 25 Jul 2018  路  2Comments  路  Source: tsedio/tsed

Description

I've wrote a few services to function as standalone services, i.e. doing some fetching and scraping, then calling some other repository services to save to DB. Point being: the service is not instantiated by a controller, just trying to instantiate in Server.ts.

The Problem

Cannot inject service into Server.ts. Can compile without error, but the instance of the service class comes back as undefined.

Steps to reproduce

  1. Create a standalone service object class in Services directory
// ShopifyScraperService.ts
import { Service } from '@tsed/common'
import { AbstractScraperService } from './AbstractScraperService'

@Service()
export class ShopifyScraperService extends AbstractScraperService {

    public startScraping = (url: string) => {
        console.log('hey, welcome to scraper service')
    }
}
  1. Inject in Server.ts and try to call class method
// Server.ts
import { GlobalAcceptMimesMiddleware, InjectorService, ServerLoader, ServerSettings } from '@tsed/common'
import '@tsed/mongoose'
import '@tsed/multipartfiles'
import { ShopifyScraperService } from './services/scraping/ShopifyScraperService'

// config @serversettings here
export class Server extends ServerLoader {
    public $onReady() {
       const shopifyScraperService: ShopifyScraperService = InjectorService.get(ShopifyScraperService)
        console.log(shopifyScraperService) // <--- WILL PRINT UNDEFINED
       shopifyScraperService.startScraping('https://scrapme.com/stuff') // <-- WILL THROW ERROR
  }
}

What I tried

  • Tried @Inject decorator above $onReady hook. Like:
    @Inject()
    $onReady(service: Service) {
          // service.doSomething()
    }

  • Tried injecting in class constructor of Server class, like:
export class Server extends ServerLoader {
 constructor(public shopifyScraperService: ShopifyScraperService){}
  • Tried injecting withInjectorService. See code sample from steps to reproduce
  • also tried this method invoked inside of a setTimeout

Thanks in advance!

question

Most helpful comment

you can grab the instance of your service like this:

export class Server extends ServerLoader {
    public $onReady() {
        const shopifyScraperService: ShopifyScraperService = this.injector.get(ShopifyScraperService)
        shopifyScraperService.startScraping('https://scrapme.com/stuff')
    }
}

All 2 comments

you can grab the instance of your service like this:

export class Server extends ServerLoader {
    public $onReady() {
        const shopifyScraperService: ShopifyScraperService = this.injector.get(ShopifyScraperService)
        shopifyScraperService.startScraping('https://scrapme.com/stuff')
    }
}

Solution from @milewski worked completely

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dorian-kwon picture dorian-kwon  路  3Comments

royibernthal picture royibernthal  路  4Comments

haroon407 picture haroon407  路  4Comments

tomer-amir-vonage picture tomer-amir-vonage  路  7Comments

S0ulan picture S0ulan  路  4Comments