sql-server - Nestjsx/crud api 在现有表上无法正常工作
问题描述
我用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) {}
}
我的客户.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);
}
}
我的customers.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 文档到设置的每一步,但找不到错误。我还尝试了 setup dto 来替换控制器设置中的实体,但得到了同样的错误。我的项目连接到 mssql 并且连接正常。
解决方案
经过数小时的搜索,解决方案是添加
params: {
CustomerId: {
field: 'CustomerID',
type: 'number',
primary: true,
},
},
在控制器中作为主键不是默认 ID。
推荐阅读
- python - 错误:您正在尝试将包含 1 层的权重文件加载到具有 14 层的模型中
- javascript - Extjs - 父组件的侦听器不会从子级触发
- arrays - Applescript - 从列表中创建一个数组并插入一个值?
- c# - 使用 RSA 现有模数和指数 C# 进行加密
- excel - 使用 Excel VBA 自动处理任何项目的数量
- rxjs - 如何正确使用 share() 运算符?
- xcode - 获取 onLongPressGesture 持续时间的值
- javascript - 用于输入类型号的 Angular 8 验证器
- python - 检查矩阵中的列是否重复并执行数字切换 - 数独 - Python
- javascript - 联系表单未向 PHP 发送下拉值