Closed yoshitamagoshi closed 1 year ago
@yoshitamagoshi
From https://docs.nestjs.com/modules
The module encapsulates providers by default. This means that it's impossible to inject providers that are neither directly part of the current module nor exported from the imported modules. Thus, you may consider the exported providers from a module as the module's public interface or API.
Initialize the TwilioModule
within the module in which you are using TwilioService
.
Hopefully, soon, I'll be publishing the isGlobal
flag which will allow you to initialize the module globally only once, and use the service through all of the modules.
Hope this helps!
With version 3.2.0
you can pass the isGlobal
property when initializing the TwilioModule
.
Example:
app.module.ts
import { TwilioModule } from 'nestjs-twilio';
@Module({
imports: [
TwilioModule.forRootAsync({
imports: [ConfigModule],
useFactory: (cfg: ConfigService) => ({
accountSid: cfg.get('TWILIO_ACCOUNT_SID'),
authToken: cfg.get('TWILIO_AUTH_TOKEN'),
}),
isGlobal: true,
inject: [ConfigService],
}),
SomeModule,
],
})
export class AppModule {}
some.module.ts
@Module({
providers: [SomeService],
})
export class SomeModule {}
some.service.ts
@Injectable()
export class SomeService {
constructor(private readonly twilioService: TwilioService) {}
async getHello(): Promise<any> {
return this.twilioService.client.messages.create({
body: 'SMS Body, sent to the phone!',
from: 'FROM_PHONE_NUMBER',
to: 'TO_PHONE_NUMBER',
});
}
}
Anyhow, I'd advise you to read the previous comment and the linked resource
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Bug Report
Describe the Bug
Thanks for the package! I'm getting the following error when attempting to import
TwilioModule
into another module using the service:How to Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
TwilioModule.forRootAsync...
as defined inapp.module.ts
TwilioService
to the constructor of a serviceTwilioModule
in the*.module.ts
for that serviceExpected Behavior
No dependency injection error
Error
See above.
Your Environment
Error reproducing steps
Please explain how did your error ocurr, you can also leave gists, repos or any kind of codebase.
Additional Information
Any other information about the problem here.