当我运行它的时候,代码不会在终端中产生错误!但是当我在postman中做Post Request的时候,终端会产生这个错误:
{"statusCode":500,"message":"Internal server error"}
在我的控制器中,这是@Post:
@Post('login')
async login(
@Body() body: LoginDto,
@Res({passthrough: true}) response: Response
) {
const user = await this.authService.findOneBy(body.email);
if(!user){
throw new BadRequestException("Email does not exist");
}
if(!await bcrypt.compare(body.password, user.password)){
throw new BadRequestException("Password does not match");
}
const jwt = await this.jwtService.signAsync({id: user.id});
response.cookie('jwt', jwt, {httpOnly: true});
return {
user
}
}
这是文件loginDto:
import { IsEmail, IsNotEmpty } from "class-validator";
export class LoginDto{
@IsNotEmpty()
@IsEmail()
email: string;
@IsNotEmpty()
password: string;
}
这是用于查找用户的@Injectable服务:
@Injectable()
export class AuthService {
constructor(@InjectRepository(UserEntity) private readonly userRepository: Repository<UserEntity>){
}
async create(user: User): Promise<User>{
return await this.userRepository.save(user);
}
async findOneBy(condition): Promise<User>{
return await this.userRepository.findOne(condition);
}
}
这是具有TypeORM的实体
import { Exclude } from "class-transformer";
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
@Entity()
export class UserEntity{
@PrimaryGeneratedColumn()
id: number;
@Column()
first_name: string;
@Column()
last_name: string;
@Column({unique: true})
email: string;
@Column()
@Exclude()
password: string;
}
我试图找出错误whith尝试和捕捉:
try{
if(!user){
throw new BadRequestException("Email does not exist");
}
if(!await bcrypt.compare(body.password, user.password)){
throw new BadRequestException("Password does not match");
}
const jwt = await this.jwtService.signAsync({id: user.id});
response.cookie('jwt', jwt, {httpOnly: true});
} catch (error){
throw error
}
终端中显示的内容如下:
[Nest] 20445 - 25/11/2022 15:54:19错误[ExceptionsHandler]您必须提供选择条件才能查找单行。错误:您必须提供选择条件才能查找单个行。在实体管理器中。findOne(/Users/Nicolas/Desktop/NestJS/nest-auth/src/实体管理器/实体管理器。ts:1094:19)在存储库中。findOne
1条答案
按热度按时间6yoyoihd1#
问题是我使用的是较新版本的Typeorm(3.1),而NestJS(9.1.4)的最新版本,我已经读到他们放弃了对ormconfig.{json,xml}文件的支持。所以你需要使用数据源将数据库连接选项放在一个{ts,js}文件中并导出它。
对于mi,有效的解决方案是,将typeform降级为0.2.45:npm i类型表@0.2.45;
另一种解决方案是:根据新文档更新代码:键入表单文档
解决方案如下:路易斯·维尼修斯