Closed dgastudio closed 4 months ago
You probably see the error because you either put ClsService
into the providers
array of some module or you constuct it manually with new
somewhere.
It is not in the migration guide because it has never been a suported way of registering it in a module. Doing so would result in a runtime error (see https://github.com/Papooch/nestjs-cls/issues/79), therefore I added an explicit check to the constructor to throw an error early in the bootstrap phase (introduced in 4.0.2
in this PR https://github.com/Papooch/nestjs-cls/pull/113).
If you believe you don't use it incorrectly and still get that error, please share a minimal reproducible example so I can investigate.
@dgastudio Were you able to find the issue, or do you need further assistance?
@Papooch sorry for late response. yes, you was right, i have used cls cervice inside app module providers thank you very much!
I believe there's some unexpected behavior triggered by this change. I have a standard set up:
//app.module.ts
imports: [
ClsModule.forRoot({
middleware: {
mount: true,
},
global: true,
}),
Then if I need to inject it somewhere I import it in the module that contains the class where ClsService will be injected:
//some.module.ts
@Module({
imports: [PrismaModule, ClsService],
....
})
//some.controller.ts
constructor(
private readonly clsService: ClsService,
) {}
This was working fine in 3.X, but with 4.X I'm getting the same error as dgastudio. I'm not adding it to the providers section nor manually constructing it. Am I doing something wrong?
Thanks
@enbits You're listing ClsService
in imports
- that's wrong, only modules should go there.
I also see you marked ClsModule.forRoot
as global
. That means ClsService
is available for injection across the whole app without the need to import the ClsModule
anywhere else.
So the fix is to remove ClsService
from imports
and optionally replace it with ClsModule
(which is not needed as long as the forRoot registration is global) and it should work as intended.
Cannot create ClsService because no AsyncLocalStorage instance was provided. Please make sure that ClsService is only provided by the ClsModule and not constructed manually or added to the providers array.
i have checked the mgiration guide, there are no mentions about this
any advice? thank you