我有一个名为“problems”的服务,它处理CRUD操作。现在,在服务中,当我想保存一个文档时,我有这个错误:
ValidationError: Problem validation failed: creator: Path `creator` is required., description: Path `description` is required., title: Path `title` is required.
字符串
这是我的schema:
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument } from 'mongoose';
export type ProblemDocument = HydratedDocument<Problem>;
@Schema()
export class Problem {
@Prop({ required: true })
title: string;
@Prop({ required: true })
description: string;
@Prop({ required: true })
creator: string;
@Prop()
ideas: string[];
@Prop()
createdAt: Date;
@Prop()
updatedAt: Date;
@Prop()
deletedAt: Date;
}
export const ProblemSchema = SchemaFactory.createForClass(Problem);
型
在problems service中创建方法:
async create(createProblemDto: CreateProblemDto): Promise<any> {
try {
console.log(createProblemDto);
const problem = new this.ProblemModel({ createProblemDto });
return problem.save();
} catch (error) {
throw error;
}
}
型
故障问题DTO:
import { IsString, IsNotEmpty } from 'class-validator';
export class CreateProblemDto {
@IsString({ message: 'Title must be a string' })
@IsNotEmpty({ message: 'Title is required' })
title: string;
@IsString({ message: 'Description must be a string' })
@IsNotEmpty({ message: 'Description is required' })
description: string;
@IsString({ message: 'Creator must be a string' })
@IsNotEmpty({ message: 'Creator is required' })
creator: string;
}
型
这是我的请求:
POST http://localhost:3000/problems Content-Type:application/json
{“title”:“hello”,“description”:“world”,“creator”:“john_doe”}
这是控制台中的日志:
{
createProblemDto: { title: 'hello', description: 'world', creator: 'john_doe' }
}
[Nest] 17591 - 01/02/2024, 6:00:27 PM ERROR [RpcExceptionsHandler] Problem validation failed: creator: Path `creator` is required., description: Path `description` is required., title: Path `title` is required.
ValidationError: Problem validation failed: creator: Path `creator` is required., description: Path `description` is required., title: Path `title` is required.
at model.Document.invalidate (/home/arta/project/vsCode/nestjs/devProblem/problems/node_modules/.pnpm/[email protected]/node_modules/mongoose/lib/document.js:3187:32)
at /home/arta/project/vsCode/nestjs/devProblem/problems/node_modules/.pnpm/[email protected]/node_modules/mongoose/lib/document.js:2980:17
at /home/arta/project/vsCode/nestjs/devProblem/problems/node_modules/.pnpm/[email protected]/node_modules/mongoose/lib/schemaType.js:1368:9
at processTicksAndRejections (node:internal/process/task_queues:77:11)
型
如果你需要更多的信息,请让我知道。
1条答案
按热度按时间qxgroojn1#
看起来你没有向
ProblemModel
构造函数传递它在服务中期望的东西。尝试将您的服务方法更改为以下内容:
字符串
或者,您可以不使用spread运算符而按原样传递参数。