1

I'm implementing an SMS service in a loopback4 application. I have my service class which has one method, but when I try calling it in a controller endpoint, after injecting the dependency and binding the service to the application, I get an error that says that it is not a function although typescript asks me for the 2 parameters that it requires, how can I fix this?

This is the service class

@bind({scope: BindingScope.TRANSIENT})
export class NexmoService {
  constructor() {}

  sendSMS(to: any, message: string){
    nexmo.message.sendSms(from, to , message, {
      type: 'unicode'
    }, (err: any, responseData: any)=> {
      if (err){
        console.log(err);
      } else {
        if ( responseData.messages[0]['status'] === '0') {
          console.log('Message sent successfully.');
        } else {
          console.log(`Message failed with error: ${responseData.messages[0]['error-text']}`)
        }
      }
    })
  }
}

Binding to the application context

import {NexmoService} from './services/nexmo.service'

export class SmsServiceApplication extends BootMixin(
  ServiceMixin(RepositoryMixin(RestApplication)),
) {
  constructor(options: ApplicationConfig = {}) {
    super(options);
    this.bind('service.sms').to(NexmoService);

This is the controller in which I use the service

import {NexmoService} from '../services/nexmo.service';


export class SmsController {
  constructor(
    @inject('service.sms')
    public smsService: NexmoService,
  ) {}

  @post('/sendSms', {
    responses: {
      '200': {
        description: 'Send SMS',
        content: {
          'application/json': {
            schema: {
              type: 'object',
              properties: {
                to: {
                  type: 'string'
                },
                message: {
                  type: 'string'
                }
              },
            },
          },
        },
      },
    },
  })
  async sendSMS(
    @requestBody({
      content: {
        'application/json': {
          schema: {
            type: 'object',
            properties: {
              to: {
                type: 'string'
              },
              message: {
                type: 'string'
              },
            },
          },
        },
      },
    }) request: Request,
  ): Promise<object> {
    let data: any = request;
    this.smsService.sendSMS(data.to, data.message)
    return {
      to: data.to,
      message: data.message
    }
  }
}

And the error

Unhandled error in POST /sendSms: 500 TypeError: this.smsService.foo is not a function
    at SmsController.sendSMS (/Users/Sergio/smsService/sms-service/src/controllers/sms.controller.ts:63:21)
    at invokeTargetMethod (/Users/Sergio/smsService/sms-service/node_modules/@loopback/context/src/invocation.ts:255:47)
    at /Users/Sergio/smsService/sms-service/node_modules/@loopback/context/src/invocation.ts:232:12
    at Object.transformValueOrPromise (/Users/Sergio/smsService/sms-service/node_modules/@loopback/context/src/value-promise.ts:298:12)
    at invokeTargetMethodWithInjection (/Users/Sergio/smsService/sms-service/node_modules/@loopback/context/src/invocation.ts:227:10)
    at InterceptedInvocationContext.invokeTargetMethod (/Users/Sergio/smsService/sms-service/node_modules/@loopback/context/src/invocation.ts:118:14)
    at targetMethodInvoker (/Users/Sergio/smsService/sms-service/node_modules/@loopback/context/src/interceptor.ts:349:23)
    at /Users/Sergio/smsService/sms-service/node_modules/@loopback/context/src/interceptor-chain.ts:218:14
    at Object.transformValueOrPromise (/Users/Sergio/smsService/sms-service/node_modules/@loopback/context/src/value-promise.ts:298:12)
    at GenericInterceptorChain.invokeNextInterceptor (/Users/Sergio/smsService/sms-service/node_modules/@loopback/context/src/interceptor-chain.ts:213:12)
    at GenericInterceptorChain.next (/Users/Sergio/smsService/sms-service/node_modules/@loopback/context/src/interceptor-chain.ts:201:17)
    at GenericInterceptorChain.invokeInterceptors (/Users/Sergio/smsService/sms-service/node_modules/@loopback/context/src/interceptor-chain.ts:178:17)
    at Object.invokeInterceptors (/Users/Sergio/smsService/sms-service/node_modules/@loopback/context/src/interceptor-chain.ts:250:16)
    at /Users/Sergio/smsService/sms-service/node_modules/@loopback/context/src/interceptor.ts:351:14
    at tryCatchFinally (/Users/Sergio/smsService/sms-service/node_modules/@loopback/context/src/value-promise.ts:222:14)
    at Object.tryWithFinally (/Users/Sergio/smsService/sms-service/node_modules/@loopback/context/src/value-promise.ts:197:10)
SergioGaitan
  • 286
  • 3
  • 11

1 Answers1

1

You have issue with your binding Here in

this.bind('service.sms').to(NexmoService);

Just have to bind the service to your service classs as

   this.bind('service.sms').toClass(NexmoService);

This will resolve the issue. Happy Loopbacking:)

Madaky
  • 389
  • 1
  • 9