【问题标题】:NestJs : How to implement a Controller using nestjsx/crud and manual implementation togetherNestJs:如何使用nestjsx/crud和手动实现一起实现控制器
【发布时间】:2020-05-20 22:55:15
【问题描述】:

在我的 Nestjs 应用下,我使用 @nestjsx/crud 来处理 crud 操作

但是对于某些路径和某些情况,我需要手动实现对服务的调用,这会运行一些 sql 查询。

我的控制器看起来像这样

import {Controller, Get} from '@nestjs/common';
import { Crud, CrudController } from '@nestjsx/crud';
import { TodoEntity } from './todo.entity';
import { TodoService } from './todo.service';

@Crud({
  model: {
    type: TodoEntity
  }
})

@Controller('todo')
export class TodoController implements CrudController<TodoEntity> {
  constructor(public todoService: TodoService) {}

  // I WANT TO DO THIS in addition to the previous tremanet->
  @Get('/mypath')
  public async myManulaTreatment() {
    return await this.todoService.getManual();
  }
}

我的服务如下所示:

import { Injectable } from '@nestjs/common';
import { TypeOrmCrudService } from "@nestjsx/crud-typeorm";
import { TodoEntity } from './todo.entity';
import { InjectRepository } from '@nestjs/typeorm';
import {getManager , getConnection} from "typeorm";

@Injectable()
export class TodoService extends TypeOrmCrudService<TodoEntity>{
  constructor(@InjectRepository(TodoEntity) repo) {
    super(repo)
  }

  // THIS IS MY MANUAL TREATMENT
  async getManual(){
    const entityManager = await getConnection().manager.query("SELECT * FROM "+process.env.DATABASE_DB.mytable);
  }
}

但我收到了这个错误:

Class 'TodoController' incorrectly implements interface 'CrudController<TodoEntity>'.
  Property 'service' is missing in type 'TodoController' but required in type 'CrudController<TodoEntity>'.

建议?

【问题讨论】:

    标签: javascript typescript nestjs typeorm


    【解决方案1】:

    而不是调用你的TodoService todoService,你应该调用它service 并确保将它传递给super(service) 以确保基类也具有正确的依赖关系。 Here's the docs for adding-routes from nestjsx/crud

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-30
      • 2014-08-25
      • 1970-01-01
      • 2019-12-15
      • 1970-01-01
      • 2020-03-21
      • 2021-01-02
      • 2018-02-15
      相关资源
      最近更新 更多