lkaric / nestjs-twilio

Injectable Twilio client for Nestjs.
https://www.npmjs.com/package/nestjs-twilio
MIT License
42 stars 10 forks source link

ERROR [ExceptionHandler] Nest can't resolve dependencies of the TwilioService #67

Closed ShiyuCheng2018 closed 3 months ago

ShiyuCheng2018 commented 5 months ago

Hello, I followed the instruction to config this module, however I get this error:

[Nest] 38  - 04/22/2024, 5:24:52 PM   ERROR [ExceptionHandler] Nest can't resolve dependencies of the TwilioService (?). Please make sure that the argument "CONFIGURABLE_MODULE_OPTIONS[c4f2b6ddebd9a3c7797af]" at index [0] is available in the TwilioModule context.
2024-04-22T17:24:52.715644801Z 
2024-04-22T17:24:52.715646259Z Potential solutions:
2024-04-22T17:24:52.715647093Z - Is TwilioModule a valid NestJS module?
2024-04-22T17:24:52.715647843Z - If "CONFIGURABLE_MODULE_OPTIONS[c4f2b6ddebd9a3c7797af]" is a provider, is it part of the current TwilioModule?
2024-04-22T17:24:52.715648926Z - If "CONFIGURABLE_MODULE_OPTIONS[c4f2b6ddebd9a3c7797af]" is exported from a separate @Module, is that module imported within TwilioModule?
2024-04-22T17:24:52.715649759Z   @Module({
2024-04-22T17:24:52.715650426Z     imports: [ /* the Module containing "CONFIGURABLE_MODULE_OPTIONS[c4f2b6ddebd9a3c7797af]" */ ]
2024-04-22T17:24:52.715651176Z   })
2024-04-22T17:24:52.715651801Z 
2024-04-22T17:24:52.715652551Z Error: Nest can't resolve dependencies of the TwilioService (?). Please make sure that the argument "CONFIGURABLE_MODULE_OPTIONS[c4f2b6ddebd9a3c7797af]" at index [0] is available in the TwilioModule context.
2024-04-22T17:24:52.715653468Z 
2024-04-22T17:24:52.715654093Z Potential solutions:
2024-04-22T17:24:52.715654759Z - Is TwilioModule a valid NestJS module?
2024-04-22T17:24:52.715655468Z - If "CONFIGURABLE_MODULE_OPTIONS[c4f2b6ddebd9a3c7797af]" is a provider, is it part of the current TwilioModule?
2024-04-22T17:24:52.715656218Z - If "CONFIGURABLE_MODULE_OPTIONS[c4f2b6ddebd9a3c7797af]" is exported from a separate @Module, is that module imported within TwilioModule?
2024-04-22T17:24:52.715657009Z   @Module({
2024-04-22T17:24:52.715657634Z     imports: [ /* the Module containing "CONFIGURABLE_MODULE_OPTIONS[c4f2b6ddebd9a3c7797af]" */ ]
2024-04-22T17:24:52.715658343Z   })
2024-04-22T17:24:52.715658968Z 
2024-04-22T17:24:52.715659593Z     at Injector.lookupComponentInParentModules (/app/node_modules/@nestjs/core/injector/injector.js:254:19)
2024-04-22T17:24:52.715682718Z     at processTicksAndRejections (node:internal/process/task_queues:95:5)
2024-04-22T17:24:52.715684384Z     at Injector.resolveComponentInstance (/app/node_modules/@nestjs/core/injector/injector.js:207:33)
2024-04-22T17:24:52.715685676Z     at resolveParam (/app/node_modules/@nestjs/core/injector/injector.js:128:38)
2024-04-22T17:24:52.715686384Z     at async Promise.all (index 0)
2024-04-22T17:24:52.715687051Z     at Injector.resolveConstructorParams (/app/node_modules/@nestjs/core/injector/injector.js:143:27)
2024-04-22T17:24:52.715687801Z     at Injector.loadInstance (/app/node_modules/@nestjs/core/injector/injector.js:70:13)
2024-04-22T17:24:52.715689093Z     at Injector.loadProvider (/app/node_modules/@nestjs/core/injector/injector.js:97:9)
2024-04-22T17:24:52.715690176Z     at Injector.lookupComponentInImports (/app/node_modules/@nestjs/core/injector/injector.js:289:17)
2024-04-22T17:24:52.715691093Z     at Injector.lookupComponentInParentModules (/app/node_modules/@nestjs/core/injector/injector.js:252:33)

it bugged me for a while, can someone help me ? Thank you!

my usage:

/auth.module.ts

import { Module } from '@nestjs/common';
import { UserController } from './controllers/user.controller';
import { UserService } from './services/user.service';
import { PrismaService } from 'src/common/services/prisma.service';
import {ConfigModule, ConfigService} from "@nestjs/config";
import {TwilioModule} from "nestjs-twilio";

@Module({
  controllers: [UserController],
  imports: [ TwilioModule.forRootAsync({
    imports: [ConfigModule],
    useFactory: (cfg: ConfigService) => ({
      accountSid: cfg.get('TWILIO_ACCOUNT_SID'),
      authToken: cfg.get('TWILIO_AUTH_TOKEN'),
    }),
    inject: [ConfigService],
  }),],
  providers: [UserService, PrismaService],
  exports: [UserService],
})
export class UserModule {}

/user.service.ts

export class UserService implements IUserService{
  constructor(private readonly prismaService: PrismaService, private readonly twilioService: TwilioService) {}

  public async generateOTP(data: userPhoneNumbers): Promise<sms>{
      // TODO: implement sms sending logic
    // generate 6 digit OTP

     const res = await this.twilioService.client.messages.create({
          body: 'SMS Body, sent to the phone!',
          from: "12365856765",
          to: `${data.country_id}${data.user_phone_number}`,
      });

     console.log(res);
    const otp = Math.floor(100000 + Math.random() * 900000).toString();
    // store the OTP in the database
    return this.prismaService.sms.create({
      data: {
        user_phone_number_id: data.id,
        code: otp,
        code_expiration_date: new Date(new Date().getTime() + 30 * 60000)
      }
    });
  }

}
alternatefaraz commented 4 months ago

I was having simillar error while importing the module in app.module and using it in another module. My way of running it was to import twillo in my notification module directly instead of app module. Then it worked. image

stale[bot] commented 3 months ago

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.