验证错误:所需路径 Mongoose NestJs

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

我有一个名为“problems”的服务,它处理 CRUD 操作。 现在,在服务中,当我想保存文档时,出现以下错误:

ValidationError: Problem validation failed: creator: Path `creator` is required., description: Path `description` is required., title: Path `title` is required.

这是我的架构:

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);

在问题服务中创建方法:

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;
}

这是我的要求:

发布http://localhost:3000/问题 内容类型:application/json

{ “标题”:“你好”, “描述”:“世界”, “创建者”:“约翰多伊” }

这是控制台中的日志:

{
  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)

如果您需要更多信息,请告诉我。

node.js mongodb mongoose nestjs
2个回答
0
投票

看起来您没有将其在服务中期望的内容传递给

ProblemModel
构造函数。

尝试将您的服务方式更改为以下方式:

async create(createProblemDto: CreateProblemDto): Promise<any> {
      try {
          console.log(createProblemDto);
          const problem = new this.ProblemModel({ ...createProblemDto });
          return problem.save();
    } catch (error) {
      throw error;
    }
 }

或者,您可以按原样传递参数,而不使用扩展运算符。


0
投票

create() 的参数列表中缺少

@Body()
装饰器。

尝试一下,它应该可以工作:

async create(@Body() createProblemDto: CreateProblemDto): Promise<any> {
...
}
© www.soinside.com 2019 - 2024. All rights reserved.