Nestjsx/crud api 在现有表上无法正常工作

问题描述 投票:0回答:2

我使用 Nestjsx 构建 Nextjs 项目来创建 Restful api。 我的客户.controller.ts

import { Controller } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { Crud, CrudController } from '@nestjsx/crud';

import { CustomersService } from './customers.service';
import { Customer } from './entities/customer.entity';

@Crud({
  model: {
    type: Customer,
  },
})
@ApiTags('Customer')
@Controller('customers')
export class CustomersController implements CrudController<Customer> {
  constructor(public service: CustomersService) {}
}

我的客户.service.ts

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { TypeOrmCrudService } from '@nestjsx/crud-typeorm';

import { Customer } from './entities/customer.entity';

@Injectable()
export class CustomersService extends TypeOrmCrudService<Customer> {
  constructor(@InjectRepository(Customer) repo) {
    super(repo);
  }
}

我的客户.controller.ts

import { Controller } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { Crud, CrudController } from '@nestjsx/crud';

import { CustomersService } from './customers.service';
import { Customer } from './entities/customer.entity';

@Crud({
  model: {
    type: Customer,
  },
})
@ApiTags('Customer')
@Controller('customers')
export class CustomersController implements CrudController<Customer> {
  constructor(public service: CustomersService) {}
}

客户.实体.ts

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
} from 'typeorm';

@Entity('tbl_Customers')
export class Customer {
  @PrimaryGeneratedColumn({ type: 'int' })
  CustomerID!: number;

  @Column({ type: 'nvarchar', length: 50 })
  CustomerName!: string;

  @Column({ type: 'nvarchar', length: 50 })
  BillingAddressLine1!: string;

  @Column({ type: 'nvarchar', length: 50 })
  BillingAddressLine2: string | null = null;

  @Column({ type: 'nvarchar', length: 50 })
  City!: string;

  @Column({ type: 'nvarchar', length: 8 })
  PostalCode!: string;
}

仅 Get /customers 工作,其他 api 端点将返回

Error: Invalid column name 'undefined'.
我遵循从 Nestjsx 文档到设置的每一步,但找不到错误。我还尝试设置 dto 来替换控制器设置中的实体,但得到相同的错误。我的项目连接到 mssql 并且连接正常。

sql-server api nestjs nestjs-swagger
2个回答
1
投票

经过几个小时的搜索,解决方案是添加

params: {
    CustomerId: {
      field: 'CustomerID',
      type: 'number',
      primary: true,
    },
  },

在控制器中,主键不是默认 ID。


0
投票

在我们的例子中,这是因为我们扩展了 get 方法

@Override('getOneBase')
  getOneAndDoStuff(
    @ParsedRequest() req: CrudRequest,
  ) {
    return this.base.getOneBase(req);
  }

https://github.com/nestjsx/crud/issues/56

中提到的已接受答案
© www.soinside.com 2019 - 2024. All rights reserved.