将服务添加到 loopback4 应用程序和服务 class 方法不适用于控制器端点
Adding a service to a loopback4 app and service class methods not working on controller endpoints
我正在 loopback4 应用程序中实现 SMS 服务。我的服务 class 有一个方法,但是当我尝试在控制器端点调用它时,在注入依赖项并将服务绑定到应用程序之后,我收到一条错误消息,指出它不是一个函数,尽管typescript 要求我提供它需要的 2 个参数,我该如何解决这个问题?
这是服务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']}`)
}
}
})
}
}
绑定到应用程序上下文
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);
这是我使用服务的控制器
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
}
}
}
错误
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)
您的绑定有问题
this.bind('service.sms').to(NexmoService);
只需将服务绑定到您的服务类即可
this.bind('service.sms').toClass(NexmoService);
这将解决问题。愉快的环回:)
我正在 loopback4 应用程序中实现 SMS 服务。我的服务 class 有一个方法,但是当我尝试在控制器端点调用它时,在注入依赖项并将服务绑定到应用程序之后,我收到一条错误消息,指出它不是一个函数,尽管typescript 要求我提供它需要的 2 个参数,我该如何解决这个问题?
这是服务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']}`)
}
}
})
}
}
绑定到应用程序上下文
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);
这是我使用服务的控制器
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
}
}
}
错误
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)
您的绑定有问题
this.bind('service.sms').to(NexmoService);
只需将服务绑定到您的服务类即可
this.bind('service.sms').toClass(NexmoService);
这将解决问题。愉快的环回:)