【发布时间】: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)
【问题讨论】:
-
你可以使用类转换器 => github.com/typestack/class-transformer#plaintoclass。我从未使用过 sequelize-typescript 我个人使用 TypeORM,我使用 class-transformer 将我的 DTO 转换为实体
标签: typescript graphql sequelize.js nestjs