loopbackjs - 如何在 LoopBack 4 中直接添加控制器?
问题描述
编辑:这只是环回的一个错误,我已经提交了 PR。
以下代码不起作用:
// BAD CODE
import {ApplicationConfig} from '@loopback/core';
import {RestApplication, RestServer, get} from '@loopback/rest';
export class HelloController {
@get('/hello')
hello(): string {
return 'Hello world!';
}
}
export class HelloWorldApplication extends RestApplication {
constructor(options: ApplicationConfig = {}) {
super(options);
this.controller(HelloController); // Error Here
}
async start() {
await super.start();
const rest = await this.getServer(RestServer);
console.log(
`REST server running on localhost:${await rest.get('rest.port')}`,
);
}
}
我收到此错误:
UnhandledPromiseRejectionWarning: TypeError: paramTypes is not iterable at resolveControllerSpec (/Users/seph/Code/Project/api-loopback/node_modules/@loopback/openapi-v3/src/controller-spec.ts:312:21)
如何在 LoopBack 4 中直接添加控制器?
解决方案
不确定这是否有帮助,但您可以通过当前控制器的构造函数注入另一个控制器。您还应该像使用 lb4 控制器一样生成控制器,然后假设您创建了 2 个控制器并希望在您可以执行的第二个控制器中访问其中的第一个:
constructor(
// Inject other controller
@inject('controllers.SomeOtherController')
// Create localy
public otherCtrl: SomeOtherController,
接着
otherCtrl.SomeMethod()
阅读更多环回控制器
问候,
推荐阅读
- javascript - 如何在框架/框中的同一页面内定向href
- scala - Scala3 宏调用 TypeTree 的类型类实例(无类型 arg)
- c - 后续的 malloc 函数可以覆盖前一个吗?
- javascript - SQL 查询递归异步问题
- javascript - 为什么命名函数不起作用?- 节点.js
- html - Angular Html Css 标头隐藏在组件之一中
- python - 我正在制作一个 mp3 播放器,我需要一个前进按钮,但似乎无法弄清楚
- mysql - sql中的关系相似度计算(也叫协同过滤user-user)
- jquery - 我想在使用 jquery 单击按钮后附加几个 div
- vue.js - 路由后组件消失