Nestjs - 整数类型的输入语法无效:"don@gmail.com"
Nestjs - invalid input syntax for type integer: "don@gmail.com"
创建登录控制器和服务时遇到标题中提到的错误。
用户table
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column('varchar', { length: 50 })
name;
@Column('varchar', { length: 50 })
surname;
@Column('varchar', { length: 50 })
street;
@Column('varchar', { length: 50 })
city;
@Column('varchar', { length: 5 })
zip;
@Column({ type: 'int', nullable: true })
rating;
@Column('varchar', { length: 10 })
phone;
@Column('date')
date;
@Column({ type: 'varchar', length: 50, nullable: false, unique: true })
email;
@Column({ type: 'varchar', length: 75, nullable: false })
password;
}
登录DTO
export class SignInDto {
@IsNotEmpty()
email: string;
@IsNotEmpty()
password: string;
}
现在,在 postman 中测试时验证有效;如果我只输入电子邮件而忽略密码,则会出现正确的错误。
另一方面,使用此 DTO 进行注册工作得很好:
export class SignUpDto {
@IsString()
@IsNotEmpty()
name: string;
@IsString()
@IsNotEmpty()
surname: string;
@IsString()
@IsNotEmpty()
street: string;
@IsString()
@IsNotEmpty()
city: string;
@MinLength(5)
@MaxLength(5)
zip: string;
rating: number;
@IsString()
@IsNotEmpty()
phone: string;
@IsDate()
@Type(() => Date)
@IsNotEmpty()
date: Date;
@IsEmail()
@IsNotEmpty()
email: string;
@IsString()
@MinLength(8)
@MaxLength(32)
@Matches(/((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/)
password: string;
}
用户存储库
import {
ConflictException,
InternalServerErrorException,
} from '@nestjs/common';
import { EntityRepository, Repository } from 'typeorm';
import { SignUpDto } from './dto/signup.dto';
import { User } from './user.entity';
import * as bcrypt from 'bcrypt';
@EntityRepository(User)
export class UsersRepository extends Repository<User> {
async createUser(signUpDto: SignUpDto): Promise<void> {
const {
name,
surname,
street,
city,
zip,
rating,
phone,
date,
email,
password,
} = signUpDto;
const salt = await bcrypt.genSalt();
const hashedPassword = await bcrypt.hash(password, salt);
const user = this.create({
name,
surname,
street,
city,
zip,
rating,
phone,
date,
email,
password: hashedPassword,
});
try {
await this.save(user);
} catch (error) {
if (error.code === '23505') {
throw new ConflictException('User with the same e-mail already exists');
} else {
console.log(error);
throw new InternalServerErrorException();
}
}
}
}
注册和登录控制器
import { Body, Controller, Post } from '@nestjs/common';
import { AuthService } from './auth.service';
import { SignInDto } from './dto/signin.dto';
import { SignUpDto } from './dto/signup.dto';
@Controller('auth')
export class AuthController {
constructor(private authService: AuthService) {}
@Post('/signup')
signUp(@Body() signUpDto: SignUpDto): Promise<void> {
return this.authService.signUp(signUpDto);
}
@Post('/signin')
signIn(@Body() signInDto: SignInDto): Promise<string> {
return this.authService.signIn(signInDto);
}
}
编写注册和登录逻辑的服务
@Injectable()
export class AuthService {
constructor(
@InjectRepository(UsersRepository)
private usersRepository: UsersRepository,
) {}
async signUp(signUpDto: SignUpDto): Promise<void> {
return this.usersRepository.createUser(signUpDto);
}
async signIn(signInDto: SignInDto): Promise<string> {
const { email, password } = signInDto;
const user = await this.usersRepository.findOne(email);
if (user && (await bcrypt.compare(password, user.password))) {
return 'success';
} else {
throw new UnauthorizedException('Check your login credentials');
}
}
}
当前数据库状态
错误示例
HTTP 请求示例
有人知道从哪里开始吗?我的意思是,我无法将电子邮件定义为整数?
TypeORM 的 findOne
在没有给出说明符的情况下基于 ID 工作。你应该做
async signIn(signInDto: SignInDto): Promise<string> {
const { email, password } = signInDto;
const user = await this.usersRepository.findOne({ email });
if (user && (await bcrypt.compare(password, user.password))) {
return 'success';
} else {
throw new UnauthorizedException('Check your login credentials');
}
}
而不是告诉 TypeORM 您正在搜索电子邮件而不是 ID
创建登录控制器和服务时遇到标题中提到的错误。
用户table
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column('varchar', { length: 50 })
name;
@Column('varchar', { length: 50 })
surname;
@Column('varchar', { length: 50 })
street;
@Column('varchar', { length: 50 })
city;
@Column('varchar', { length: 5 })
zip;
@Column({ type: 'int', nullable: true })
rating;
@Column('varchar', { length: 10 })
phone;
@Column('date')
date;
@Column({ type: 'varchar', length: 50, nullable: false, unique: true })
email;
@Column({ type: 'varchar', length: 75, nullable: false })
password;
}
登录DTO
export class SignInDto {
@IsNotEmpty()
email: string;
@IsNotEmpty()
password: string;
}
现在,在 postman 中测试时验证有效;如果我只输入电子邮件而忽略密码,则会出现正确的错误。
另一方面,使用此 DTO 进行注册工作得很好:
export class SignUpDto {
@IsString()
@IsNotEmpty()
name: string;
@IsString()
@IsNotEmpty()
surname: string;
@IsString()
@IsNotEmpty()
street: string;
@IsString()
@IsNotEmpty()
city: string;
@MinLength(5)
@MaxLength(5)
zip: string;
rating: number;
@IsString()
@IsNotEmpty()
phone: string;
@IsDate()
@Type(() => Date)
@IsNotEmpty()
date: Date;
@IsEmail()
@IsNotEmpty()
email: string;
@IsString()
@MinLength(8)
@MaxLength(32)
@Matches(/((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/)
password: string;
}
用户存储库
import {
ConflictException,
InternalServerErrorException,
} from '@nestjs/common';
import { EntityRepository, Repository } from 'typeorm';
import { SignUpDto } from './dto/signup.dto';
import { User } from './user.entity';
import * as bcrypt from 'bcrypt';
@EntityRepository(User)
export class UsersRepository extends Repository<User> {
async createUser(signUpDto: SignUpDto): Promise<void> {
const {
name,
surname,
street,
city,
zip,
rating,
phone,
date,
email,
password,
} = signUpDto;
const salt = await bcrypt.genSalt();
const hashedPassword = await bcrypt.hash(password, salt);
const user = this.create({
name,
surname,
street,
city,
zip,
rating,
phone,
date,
email,
password: hashedPassword,
});
try {
await this.save(user);
} catch (error) {
if (error.code === '23505') {
throw new ConflictException('User with the same e-mail already exists');
} else {
console.log(error);
throw new InternalServerErrorException();
}
}
}
}
注册和登录控制器
import { Body, Controller, Post } from '@nestjs/common';
import { AuthService } from './auth.service';
import { SignInDto } from './dto/signin.dto';
import { SignUpDto } from './dto/signup.dto';
@Controller('auth')
export class AuthController {
constructor(private authService: AuthService) {}
@Post('/signup')
signUp(@Body() signUpDto: SignUpDto): Promise<void> {
return this.authService.signUp(signUpDto);
}
@Post('/signin')
signIn(@Body() signInDto: SignInDto): Promise<string> {
return this.authService.signIn(signInDto);
}
}
编写注册和登录逻辑的服务
@Injectable()
export class AuthService {
constructor(
@InjectRepository(UsersRepository)
private usersRepository: UsersRepository,
) {}
async signUp(signUpDto: SignUpDto): Promise<void> {
return this.usersRepository.createUser(signUpDto);
}
async signIn(signInDto: SignInDto): Promise<string> {
const { email, password } = signInDto;
const user = await this.usersRepository.findOne(email);
if (user && (await bcrypt.compare(password, user.password))) {
return 'success';
} else {
throw new UnauthorizedException('Check your login credentials');
}
}
}
当前数据库状态
错误示例
HTTP 请求示例
有人知道从哪里开始吗?我的意思是,我无法将电子邮件定义为整数?
TypeORM 的 findOne
在没有给出说明符的情况下基于 ID 工作。你应该做
async signIn(signInDto: SignInDto): Promise<string> {
const { email, password } = signInDto;
const user = await this.usersRepository.findOne({ email });
if (user && (await bcrypt.compare(password, user.password))) {
return 'success';
} else {
throw new UnauthorizedException('Check your login credentials');
}
}
而不是告诉 TypeORM 您正在搜索电子邮件而不是 ID