Tsed: The typeORMService instances is empty on $afterRoutesInit.

Created on 21 Jan 2019  ยท  11Comments  ยท  Source: tsedio/tsed

Information

  • Version: 5.1.0
  • Type: Issue/Story

Description

The typeORMService instances is empty on $afterRoutesInit.

Example

@Service()
export class UserService {
    private repository: Repository<User>;
    constructor(private typeORMService: TypeORMService) {}

    $afterRoutesInit() {
        // this is throw error , because  this.typeORMService.get() return undefined.
        //this.repository = this.typeORMService.get().getRepository(User);
    }

    async find(email: string) {
       // Here i see the typeORMService instances . and it work well.
       this.repository = this.typeORMService.get().getRepository(User);
        return this.repository.findOne(email)
    }

}

my log:

PS C:\Users\arkadiy\Desktop\Mutual-Fund\web-server> ts-node app
[2019-01-21T08:14:37.335] [DEBUG] [TSED] - Initialize settings
[2019-01-21T08:14:37.342] [INFO ] [TSED] - settings.rootDir => C:\Users\arkadiy\Desktop\Mutual-Fund\web-server
[2019-01-21T08:14:37.342] [INFO ] [TSED] - settings.env => development
[2019-01-21T08:14:37.342] [INFO ] [TSED] - settings.httpPort => 8080
[2019-01-21T08:14:37.343] [INFO ] [TSED] - settings.httpsPort => 8000
[2019-01-21T08:14:37.343] [INFO ] [TSED] - settings.version => 1.0.0
[2019-01-21T08:14:37.343] [INFO ] [TSED] - settings.uploadDir => C:\Users\arkadiy\Desktop\Mutual-Fund\web-server/uploads
[2019-01-21T08:14:37.344] [INFO ] [TSED] - settings.logger => { requestFields: undefined,
  debug: true,
  logRequest: true,
  jsonIndentation: 2 }
[2019-01-21T08:14:37.347] [INFO ] [TSED] - settings.scope => singleton
[2019-01-21T08:14:37.347] [INFO ] [TSED] - settings.errors => { headerName: 'errors' }
[2019-01-21T08:14:37.347] [INFO ] [TSED] - settings.mount => { '/rest':
   'C:\\Users\\arkadiy\\Desktop\\Mutual-Fund\\web-server/controllers/**/*.ts' }
[2019-01-21T08:14:37.348] [INFO ] [TSED] - settings.exclude => [ '**/*.spec.ts', '**/*.spec.js' ]
[2019-01-21T08:14:37.348] [INFO ] [TSED] - settings.componentsScan => [ 'C:\\Users\\arkadiy\\Desktop\\Mutual-Fund\\web-server/middlewares/**/**.{ts,js}',
  'C:\\Users\\arkadiy\\Desktop\\Mutual-Fund\\web-server/services/**/**.{ts,js}' ]
[2019-01-21T08:14:37.349] [INFO ] [TSED] - settings.acceptMimes => [ 'application/json' ]
[2019-01-21T08:14:37.349] [INFO ] [TSED] - settings.serveStatic => { '/': 'C:\\Users\\arkadiy\\Desktop\\Mutual-Fund\\client\\dist' }
[2019-01-21T08:14:37.349] [INFO ] [TSED] - settings.passport => {}
[2019-01-21T08:14:37.349] [INFO ] [TSED] - settings.swagger => [ { doc: 'api-auth',
    path: '/api-docs-auth',
    outFile:
     'C:\\Users\\arkadiy\\Desktop\\Mutual-Fund\\swagger\\web-server/swagger-auth.json',
    hidden: false } ]
[2019-01-21T08:14:37.350] [INFO ] [TSED] - settings.typeorm => [ { name: 'default',
    type: 'mongodb',
    host: 'localhost',
    port: 27017,
    database: 'DBUsers',
    logging: true,
    entities:
     [ 'C:\\Users\\arkadiy\\Desktop\\Mutual-Fund\\web-server\\user.model.ts' ] } ]
[2019-01-21T08:14:37.350] [INFO ] [TSED] - Build services
[2019-01-21T08:14:37.351] [DEBUG] [TSED] - InjectorService loaded
[2019-01-21T08:14:37.351] [DEBUG] [TSED] - ServerSettingsService loaded
[2019-01-21T08:14:37.352] [DEBUG] [TSED] - ExpressApplication loaded
[2019-01-21T08:14:37.352] [DEBUG] [TSED] - HttpServer loaded
[2019-01-21T08:14:37.352] [DEBUG] [TSED] - HttpsServer loaded
[2019-01-21T08:14:37.353] [DEBUG] [TSED] - JsonSchemesService built
[2019-01-21T08:14:37.353] [DEBUG] [TSED] - ConverterService built
[2019-01-21T08:14:37.354] [DEBUG] [TSED] - ArrayConverter built
[2019-01-21T08:14:37.354] [DEBUG] [TSED] - DateConverter built
[2019-01-21T08:14:37.354] [DEBUG] [TSED] - MapConverter built
[2019-01-21T08:14:37.354] [DEBUG] [TSED] - PrimitiveConverter built
[2019-01-21T08:14:37.355] [DEBUG] [TSED] - SetConverter built
[2019-01-21T08:14:37.355] [DEBUG] [TSED] - SymbolConverter built
[2019-01-21T08:14:37.355] [DEBUG] [TSED] - ParseService built
[2019-01-21T08:14:37.355] [DEBUG] [TSED] - BodyParamsFilter built
[2019-01-21T08:14:37.356] [DEBUG] [TSED] - CookiesFilter built
[2019-01-21T08:14:37.356] [DEBUG] [TSED] - HeaderParamsFilter built
[2019-01-21T08:14:37.356] [DEBUG] [TSED] - LocalsFilter built
[2019-01-21T08:14:37.356] [DEBUG] [TSED] - PathParamsFilter built
[2019-01-21T08:14:37.357] [DEBUG] [TSED] - QueryParamsFilter built
[2019-01-21T08:14:37.357] [DEBUG] [TSED] - SessionFilter built
[2019-01-21T08:14:37.357] [DEBUG] [TSED] - ValidationService built
[2019-01-21T08:14:37.358] [DEBUG] [TSED] - SendResponseMiddleware built
[2019-01-21T08:14:37.358] [DEBUG] [TSED] - AuthenticatedMiddleware built from class MyAuthenticatedMiddleware
[2019-01-21T08:14:37.358] [DEBUG] [TSED] - AcceptMimesMiddleware built
[2019-01-21T08:14:37.358] [DEBUG] [TSED] - GlobalAcceptMimesMiddleware built
[2019-01-21T08:14:37.359] [DEBUG] [TSED] - GlobalErrorHandlerMiddleware built
[2019-01-21T08:14:37.359] [DEBUG] [TSED] - LogIncomingRequestMiddleware built
[2019-01-21T08:14:37.359] [DEBUG] [TSED] - ResponseViewMiddleware built
[2019-01-21T08:14:37.359] [DEBUG] [TSED] - RouteService built
[2019-01-21T08:14:37.363] [DEBUG] [TSED] - ControllerService built
[2019-01-21T08:14:37.363] [DEBUG] [TSED] - ServeStaticService built
[2019-01-21T08:14:37.364] [DEBUG] [TSED] - SwaggerService built
[2019-01-21T08:14:37.364] [DEBUG] [TSED] - SwaggerModule built
[2019-01-21T08:14:37.364] [DEBUG] [TSED] - TypeORMService built
[2019-01-21T08:14:37.365] [DEBUG] [TSED] - TypeORMModule built
[2019-01-21T08:14:37.365] [DEBUG] [TSED] - UserService built
[2019-01-21T08:14:37.365] [DEBUG] [TSED] - PassportCtrl built
[2019-01-21T08:14:37.366] [DEBUG] [TSED] - FundCtrl built
[2019-01-21T08:14:37.366] [DEBUG] [TSED] - PassportLocalService built
[2019-01-21T08:14:37.366] [DEBUG] [TSED] - Call hook $onInit
[2019-01-21T08:14:37.367] [INFO ] [TSED] - Create connection with typeorm to database: default
[2019-01-21T08:14:37.367] [DEBUG] [TSED] - options: {"name":"default","type":"mongodb","host":"localhost","port":27017,"database":"DBUsers","logging":true,"entities":["C:\\Users\\arkadiy\\Desktop\\Mutual-Fund\\web-server\\user.model.ts"]}
All services is ready
[2019-01-21T08:14:37.573] [DEBUG] [TSED] - Call hook $onInit  promises built
(node:13864) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option {
useNewUrlParser: true } to MongoClient.connect.
[2019-01-21T08:14:38.572] [DEBUG] [TSED] - Settings and injector loaded
[2019-01-21T08:14:38.573] [DEBUG] [TSED] - Mount middlewares
[2019-01-21T08:14:38.574] [DEBUG] [TSED] - Call hook $onMountingMiddlewares
[2019-01-21T08:14:38.618] [DEBUG] [TSED] - Call hook $beforeRoutesInit
Controllers and routes isn't mounted
[2019-01-21T08:14:38.619] [DEBUG] [TSED] - Call hook $onRoutesInit
[2019-01-21T08:14:38.619] [INFO ] [TSED] - Map controllers
Controllers and routes are being built
[2019-01-21T08:14:38.620] [DEBUG] [TSED] - Call hook $afterRoutesInit
[2019-01-21T08:14:38.620] [INFO ] [TSED] - Routes mounted :
[2019-01-21T08:14:38.621] [INFO ] [TSED] -
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Method         โ”‚ Endpoint                   โ”‚ Class method                   โ”‚
โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚
โ”‚ POST           โ”‚ /rest/auth/login           โ”‚ PassportCtrl.login()           โ”‚
โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚
โ”‚ POST           โ”‚ /rest/auth/isAuthenticated โ”‚ PassportCtrl.isAuthenticated() โ”‚
โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚
โ”‚ GET            โ”‚ /rest/auth/logout          โ”‚ PassportCtrl.logout()          โ”‚
โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚
โ”‚ GET            โ”‚ /rest/funds                โ”‚ FundCtrl.get()                 โ”‚
โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”‚
โ”‚ POST           โ”‚ /rest/funds/save           โ”‚ FundCtrl.post()                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
Controllers and routes are built
typeORMService:
undefined
[2019-01-21T08:14:38.630] [DEBUG] [TSED] - Start server on http://0.0.0.0:8080
[2019-01-21T08:14:38.630] [DEBUG] [TSED] - Start server on https://0.0.0.0:8000
[2019-01-21T08:14:38.633] [INFO ] [TSED] - HTTP Server listen on http://0.0.0.0:8080
[2019-01-21T08:14:38.634] [INFO ] [TSED] - HTTP Server listen on https://0.0.0.0:8000
[2019-01-21T08:14:38.634] [DEBUG] [TSED] - Call hook $onReady
Server started...
[2019-01-21T08:14:38.635] [DEBUG] [TSED] - Call hook $onServerReady
[2019-01-21T08:14:38.635] [INFO ] [TSED] - [api-auth] Swagger JSON is available on http://0.0.0.0:8080/api-docs-auth/swagger.json
[2019-01-21T08:14:38.635] [INFO ] [TSED] - [api-auth] Swagger UI is available on http://0.0.0.0:8080/api-docs-auth/
Server is ready and listen the port
[2019-01-21T08:14:38.636] [INFO ] [TSED] - Started in 3952 ms
[2019-01-21T08:14:38.652] [INFO ] [TSED] - Connected with typeorm to database: default
[2019-01-21T08:14:39.579] [WARN ] [TSED] - Timeout on $onInit hook. Promise are unfulfilled on serviceTypeORMModule In production, the warning will down the server!
bug

All 11 comments

@Romakita maybe it's the same problem as #459 ?

No, there he said that the onafterinit not called at all.

ok i'll check this problem ASAP

Hello @yantrab,

I added a working example with TypeORM on this repository, it works and I've the TypeORMService in the afterRoutesInit. You can checkout the example here:
https://github.com/Romakita/ts-express-decorators/tree/production/integration/typeorm

Try it and tell me if it works for you.
See you,
Romain

I have a similar issue, I have managed to narrow it down to the settings being empty.

import * as TYPEORM_CONNECTION_OPTIONS from './database.config'

@ServerSettings({
...
  typeorm: [TYPEORM_CONNECTION_OPTIONS],
})
export class Server extends ServerLoader {}

It seems it has something to do with how the items are loaded.

If I log the typeorm settings before the settings decorator everything is defined.

After much digging, seems ts-node 8+ has some packages that cause issues. If I have 7 it works okay?

what is the problem with ts-node 8? if it's just a compilation issue we can fix that.

I am not sure I didnโ€™t investigate it further than downgrading the package.

I have the same issue. It works perfectly if I compile it first and then run it with node dist/index.js. But it doesn't work with ts-node. Downgrade version doesn't help.

@yantrab
I see in your log:

[2019-01-21T08:14:39.579] [WARN ] [TSED] - Timeout on $onInit hook. Promise are unfulfilled on serviceTypeORMModule In production, the warning will down the server!

Hum it means the connection isn't correctly created. My error is, in development mode, the server doesn't throw an error when a promise isn't fullfilled on $onInit hook. It explain why you have an empty connection on $afterRoutesInit.

Maybe the delay is too short and the server should wait the promise resolution in any case. I'll fix this problem ASAP.

See you
Romain

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tomer-amir-vonage picture tomer-amir-vonage  ยท  7Comments

royibernthal picture royibernthal  ยท  4Comments

S0ulan picture S0ulan  ยท  4Comments

RickStanley picture RickStanley  ยท  6Comments

dorian-kwon picture dorian-kwon  ยท  3Comments