【发布时间】:2021-08-29 05:21:58
【问题描述】:
我使用nestjsx 构建我的Nestjs 项目以创建Restful api。 我的 customer.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) {}
}
我的 customer.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) {}
}
customer.entity.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;
}
只有让 /customers 工作,其他 api 端点将返回 Error: Invalid column name 'undefined'. 我按照从 nestjsx 文档的每一步进行设置,但找不到错误。我还尝试了 setup dto 来替换控制器设置中的实体,但得到了同样的错误。我的项目连接到 mssql 并且连接正常。
【问题讨论】:
标签: sql-server api nestjs nestjs-swagger