Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The typeORMService instances is empty on $afterRoutesInit. #509

Closed
yantrab opened this issue Jan 21, 2019 · 11 comments
Closed

The typeORMService instances is empty on $afterRoutesInit. #509

yantrab opened this issue Jan 21, 2019 · 11 comments
Labels
Milestone

Comments

@yantrab
Copy link

yantrab commented Jan 21, 2019

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!
@Romakita Romakita added this to the BACKLOG milestone Jan 21, 2019
@OskarLebuda
Copy link
Contributor

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

@yantrab
Copy link
Author

yantrab commented Jan 22, 2019

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

@Romakita
Copy link
Collaborator

ok i'll check this problem ASAP

@Romakita
Copy link
Collaborator

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

@varsis
Copy link

varsis commented Jan 30, 2019

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.

@varsis
Copy link

varsis commented Jan 30, 2019

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

@Romakita
Copy link
Collaborator

Romakita commented Feb 1, 2019

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

@varsis
Copy link

varsis commented Feb 1, 2019

I am not sure I didn’t investigate it further than downgrading the package.

@pori4ka
Copy link

pori4ka commented Feb 9, 2019

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.

@Romakita
Copy link
Collaborator

@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

@varsis
Copy link

varsis commented Feb 15, 2019

This might be related: TypeStrong/ts-node#754

@Romakita Romakita closed this as completed May 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants