【问题标题】:How to properly define DTO objects with postgres and sequelize in NestJs framework如何在 NestJs 框架中使用 postgres 和 sequelize 正确定义 DTO 对象
【发布时间】:2021-06-28 07:52:40
【问题描述】:

我正在尝试在 NestJS 中创建简单的解析器,但类型有问题(我是打字稿的新手)。第二天我正在努力解决它,但我不明白。

我的 users.service.ts 有问题吗,有人可以检查我做错了什么吗?

// user.model.ts

import {
  BeforeCreate,
  Column,
  DataType,
  Model,
  PrimaryKey,
  Table,
} from 'sequelize-typescript';
import { Field, ID, ObjectType } from '@nestjs/graphql';

const argon2 = require('argon2');

@ObjectType()
@Table
export class User extends Model<User> {
  @Field()
  @Column({ primaryKey: true, autoIncrement: true })
  id: number;

  @Field()
  @Column({
    type: DataType.STRING,
    allowNull: false,
  })
  firstName: string;

  @Field()
  @Column
  lastName: string;

  @Field()
  @Column
  username: string;

  @Field()
  @Column({
    type: DataType.STRING,
    allowNull: false,
  })
  password: string;

  @Field()
  @Column({
    type: DataType.STRING,
    unique: true,
    allowNull: false,
  })
  email: string;

  @Field()
  @Column({ defaultValue: true })
  isActive: boolean;

  @BeforeCreate
  static async hashPassword(user: User) {
    if (user.password) {
      user.password = await argon2.hash(user.password);
    }
  }
}
// create-user.dto.ts

import { Field, InputType } from '@nestjs/graphql';

@InputType()
export class CreateUserDto {
  @Field()
  firstName?: string;

  @Field()
  username: string;

  @Field()
  email: string;

  @Field()
  lastName?: string;

  @Field()
  password: string;
}



// auth.resolver.ts

import {
  Resolver,
  Mutation,
  Args,
  Field,
  ObjectType,
  InputType,
} from '@nestjs/graphql';
import { IsString, IsEmail, IsNotEmpty } from 'class-validator';
import { AuthService } from './auth.service';
import { User } from '../users/user.model';
import { UsersService } from '../users/users.service';
import { Payload } from '../../types/payload';

@ObjectType()
export class AuthType {
  @Field()
  @IsEmail()
  email: string;
  @Field()
  @IsString()
  @IsNotEmpty()
  token: string;
}

@InputType()
export class UsernamePasswordInput {
  @Field()
  email: string;
  @Field()
  username: string;
  @Field()
  password: string;
}

@Resolver('Auth')
export class AuthResolver {
  constructor(
    private authService: AuthService,
    private userService: UsersService,
  ) {}

  @Mutation((returns) => AuthType)
  async register(
    @Args('email') email: string,
    @Args('username') username: string,
    @Args('password') password: string,
  ) {
    const user: UsernamePasswordInput = { username, email, password };
    try {
      const response: User = await this.userService.create(user);
      const payload: Payload = {
        email: response.email,
        username: response.username,
      };

      const token = await this.authService.signPayload(payload);
      return { email: response.email, token };
    } catch (exception) {
      throw exception;
    }
  }
}
// users.service.ts

  async create(userDTO: CreateUserDto): Promise<User> {
    const { email } = userDTO;
    const user = await this.userModel.findOne({ where: { email } });
    if (user) {
      throw new HttpException('User already exists', HttpStatus.BAD_REQUEST);
    }
    // const createdUser = new this.userModel(userDTO);
    const createdUser = await this.userModel.create(userDTO);
    return await createdUser.save();
  }

在 users.service.ts 行中: const createdUser = await this.userModel.create(userDTO);

我有错误:

(parameter) userDTO: CreateUserDto
Argument of type 'CreateUserDto' is not assignable to parameter of type 'User'.
  Type 'CreateUserDto' is missing the following properties from type 'User': isActive, $add, $set, $get, and 33 more.ts(2345)

【问题讨论】:

标签: typescript graphql sequelize.js nestjs


【解决方案1】:

我从模型定义中删除了:

export class User extends Model&lt;User&gt;

现在它可以工作了。

为什么在 Nest 文档中列出了这样一个奇怪的声明,我不知道.. 谢谢

【讨论】:

    猜你喜欢
    • 2021-07-30
    • 2021-08-10
    • 2021-07-01
    • 1970-01-01
    • 2020-08-28
    • 2022-12-28
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多