FoalTS / foal

Full-featured Node.js framework 🚀
https://foalts.org/
MIT License
1.9k stars 142 forks source link

Cannot read property 'isServiceManager' of undefined #998

Closed SeveHo closed 2 years ago

SeveHo commented 3 years ago

Version of FoalTS: 2.6.0

Hi @LoicPoullain,

I have a similar problem like https://github.com/FoalTS/foal/issues/488, but not exactly the same. I import one service in an other service:

export class PaymentService {
  @dependency
  logger: LoggerService;

  @dependency
  pusher: PusherService;

  triggerNotification(userId: string): void {
    this.pusher.trigger(user.id, "update");
  }
}

and in my PusherService is simply do

export class PusherService {
  private pusher: Pusher;
  constructor() {
    this.pusher = new Pusher(REDACTED_CONFIG);
  }

  trigger(channel: string, event: string, data?: any) {
    this.pusher.trigger(channel, event, (data = {}));
  }
}

So I am not referencing circular but I still get the same error:

[1] Starting child process with 'node ./build/index.js'
[1] TypeError: Cannot read property 'isServiceManager' of undefined
[1]     at ServiceManager.get (REDACTED/node_modules/@foal/core/lib/core/service-manager.js:115:57)
[1]     at ServiceManager.get (REDACTED/node_modules/@foal/core/lib/core/service-manager.js:137:52)
[1]     at ServiceManager.get (REDACTED/node_modules/@foal/core/lib/core/service-manager.js:137:52)
[1]     at makeControllerRoutes (REDACTED/node_modules/@foal/core/lib/core/routes/make-controller-routes.js:59:33)
[1]     at makeControllerRoutes.next (<anonymous>)
[1]     at Object.makeControllerRoutes (REDACTED/node_modules/@foal/core/lib/core/routes/make-controller-routes.js:127:57)
[1]     at makeControllerRoutes.next (<anonymous>)
[1]     at Object.createApp (REDACTED/node_modules/@foal/core/lib/express/create-app.js:81:24)
[1]     at main (REDACTED/src/index.ts:41:21)
[1]     at Object.<anonymous> (REDACTED/src/index.ts:48:1)
[1]     at Module._compile (node:internal/modules/cjs/loader:1092:14)
[1]     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
[1]     at Module.load (node:internal/modules/cjs/loader:972:32)
[1]     at Function.Module._load (node:internal/modules/cjs/loader:813:14)
[1]     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
[1]     at node:internal/main/run_main_module:17:47
[1] Program node ./build/index.js exited with code 1

Do you see what I am doing wrong or is this a bug? Weirdly only the PusherService is causing this problem but the LoggerService works fine.

LoicPoullain commented 3 years ago

Hi @SeveHo 👋

I'm not sure that the problem comes from the injection of the PusherService. Is it possible that you have circular ES6 imports? For example the file owning the PusherService imports the constant REDACTED_CONFIG from a file X which imports directly or indirectly something from the file PaymentService?

SeveHo commented 3 years ago

Hey, if I disable the injection everything works fine. Also the config is just an object with string attributes.

LoicPoullain commented 3 years ago

Do you use default exports or named exports? If you use default exports, moving to named exports might solve the problem.

SeveHo commented 3 years ago

Named exports. Weirdly changing the services/index.ts from

export { PusherService } from "./pusher.service";

to

import { PusherService } from "./pusher.service";
export { PusherService };

resolves the error. Can you explain why that is?

LoicPoullain commented 3 years ago

Unfortunately, I don't really know where it comes from (maybe a change in your tsconfig.json ?). It's like PusherService was found as undefined when providing it with @dependency push: PusherService.

LoicPoullain commented 2 years ago

I'm closing the issue as it has been resolved with the solution you found. I'm sorry that I could not find out what happened though 😕