在服务 nestJS 中使用时 DTO 不起作用
DTO not working when used in service nestJS
我有一个像
这样的DTO
export class CreatePlotDTO {
@IsNumber()
@ApiProperty()
@IsOptional()
area: number;
@IsNumber()
@ApiProperty()
@IsOptional()
ownerID: number;
}
和创建方法
更新:这是在 plotService 中并接受上面的 dto
async createPlot(plot: CreatePlotDTO) {
return this.plotModel.create(plot)
}
这已经运行了最长时间,但现在由于此错误而失败
Argument of type 'CreatePlotDTO' is not assignable to parameter of type 'CreationAttributes<Plot>'.
Type 'CreatePlotDTO' is not assignable to type 'Omit<any, string>'.
Index signature is missing in type 'CreatePlotDTO'.
我怀疑问题出在“sequelize”:“^6.16.2”或“sequelize-typescript”:“^2.1.3”。
有解决此问题的想法吗?
同样的版本也发生在我身上(“sequelize”:“^6.16.2”或“sequelize-typescript”:“^2.1.3”)。我怀疑这两者之一有问题,但作为一种解决方法,我做了类似以下的事情:
- 像这样定义你的模型:
@Table
export class Plot extends Model<Plot> {
...
}
(注意 <Plot>
部分)。
- 将您的 DTO 实例转换为实际模型类型:
async createPlot(plot: CreatePlotDTO) {
return this.plotModel.create(plot as Plot)
}
(注意 as Plot
部分)。
我有一个像
这样的DTOexport class CreatePlotDTO {
@IsNumber()
@ApiProperty()
@IsOptional()
area: number;
@IsNumber()
@ApiProperty()
@IsOptional()
ownerID: number;
}
和创建方法 更新:这是在 plotService 中并接受上面的 dto
async createPlot(plot: CreatePlotDTO) {
return this.plotModel.create(plot)
}
这已经运行了最长时间,但现在由于此错误而失败
Argument of type 'CreatePlotDTO' is not assignable to parameter of type 'CreationAttributes<Plot>'.
Type 'CreatePlotDTO' is not assignable to type 'Omit<any, string>'.
Index signature is missing in type 'CreatePlotDTO'.
我怀疑问题出在“sequelize”:“^6.16.2”或“sequelize-typescript”:“^2.1.3”。 有解决此问题的想法吗?
同样的版本也发生在我身上(“sequelize”:“^6.16.2”或“sequelize-typescript”:“^2.1.3”)。我怀疑这两者之一有问题,但作为一种解决方法,我做了类似以下的事情:
- 像这样定义你的模型:
@Table
export class Plot extends Model<Plot> {
...
}
(注意 <Plot>
部分)。
- 将您的 DTO 实例转换为实际模型类型:
async createPlot(plot: CreatePlotDTO) {
return this.plotModel.create(plot as Plot)
}
(注意 as Plot
部分)。