You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NestJS GraphQL中RegisterDto参数为空问题求助

问题

定义了RegisterDto和LoginDto两个DTO,LoginDto在登录Resolver中能正常接收参数,但RegisterDto在注册Mutation中无法正常通过验证,执行注册请求时抛出Bad Request错误。

相关代码如下:

DTO代码

export class RegisterDto {
  @Field()
  @IsNotEmpty({ message: 'Fullname is required' })
  @IsString({ message: 'Fullname must be a string' })
  fullname: string;

  @Field()
  @IsNotEmpty({ message: 'Password is required' })
  @MinLength(8, { message: 'Fullname must be a string' })
  password: string;

  // @Field()
  // @IsNotEmpty({ message: 'Confirm Password is required' })
  // confirmPassword: string;

  @Field()
  @IsNotEmpty({ message: 'Email is required' })
  @IsEmail({}, { message: 'Fullname must be a string' })
  email: string;
}

export class LoginDto {
  @Field()
  @IsNotEmpty({ message: 'Email is required' })
  @IsEmail({}, { message: 'Email must be valid' })
  email: string;

  @Field()
  @IsNotEmpty({ message: 'Email is required' })
  password: string;
}

登录Resolver代码

@Mutation(() => User)
async login(
  @Args('loginInput') loginDto: LoginDto,
  @Context() context: { req: Request; res: Response },
) {
  return this.authService.login(loginDto, context.res); 
}

注册Resolver代码

@Mutation(() => User)
async register(
  @Args('registerDto') registerDto: RegisterDto,
  @Context() context: { req: Request; res: Response },
) {
  const { user } = await this.authService.register(registerDto, context.res);
  return { user };
}

执行的注册Mutation

mutation {
  register(registerDto:{email:"asd",fullname:"asd",password:"asd"}){
    fullname
  }
}

抛出的错误

"originalError": {
          "message": [
            "Fullname must be a string",
            "Fullname must be a string"
          ],
          "error": "Bad Request",
          "statusCode": 400
        }
解决方法

1. 修正验证规则的错误提示信息

RegisterDto存在复制粘贴导致的提示信息混淆问题,这会干扰错误排查:

  • password字段的@MinLength提示错误写成了Fullname must be a string,应改为Password must be at least 8 characters long
  • email字段的@IsEmail提示错误也写成了Fullname must be a string,应改为Email must be a valid email address

修正后的RegisterDto:

export class RegisterDto {
  @Field()
  @IsNotEmpty({ message: 'Fullname is required' })
  @IsString({ message: 'Fullname must be a string' })
  fullname: string;

  @Field()
  @IsNotEmpty({ message: 'Password is required' })
  @MinLength(8, { message: 'Password must be at least 8 characters long' })
  password: string;

  @Field()
  @IsNotEmpty({ message: 'Email is required' })
  @IsEmail({}, { message: 'Email must be a valid email address' })
  email: string;
}

2. 修正请求参数的合法性

当前执行的Mutation参数不符合验证规则:

  • email:"asd"不是有效邮箱格式,需改为规范格式(如asd@example.com)
  • password:"asd"长度仅3位,不符合@MinLength(8)要求,需改为至少8位的密码

修正后的Mutation示例:

mutation {
  register(registerDto:{email:"asd@example.com",fullname:"asd",password:"asd12345"}){
    fullname
  }
}

3. 修正Resolver返回值类型不匹配问题

注册Resolver定义的返回类型是User,但代码返回的是{ user }(包含user属性的对象),会导致GraphQL类型校验失败,需直接返回user对象:

@Mutation(() => User)
async register(
  @Args('registerDto') registerDto: RegisterDto,
  @Context() context: { req: Request; res: Response },
) {
  const { user } = await this.authService.register(registerDto, context.res);
  return user;
}

4. 修正LoginDto的错误提示(可选)

LoginDto中password字段的@IsNotEmpty提示写成了Email is required,建议一并修正:

export class LoginDto {
  @Field()
  @IsNotEmpty({ message: 'Email is required' })
  @IsEmail({}, { message: 'Email must be valid' })
  email: string;

  @Field()
  @IsNotEmpty({ message: 'Password is required' })
  password: string;
}

内容的提问来源于stack exchange,提问作者MiguelG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 18:06:31