首页 > 解决方案 > 如何在 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 中直接添加控制器?

标签: loopbackjs

解决方案


不确定这是否有帮助,但您可以通过当前控制器的构造函数注入另一个控制器。您还应该像使用 lb4 控制器一样生成控制器,然后假设您创建了 2 个控制器并希望在您可以执行的第二个控制器中访问其中的第一个:

constructor(   
// Inject other controller
@inject('controllers.SomeOtherController')
// Create localy
public otherCtrl: SomeOtherController,

接着

otherCtrl.SomeMethod()

阅读更多环回控制器

问候,


推荐阅读