【发布时间】:2023-01-31 17:24:13
【问题描述】:
我正在编写一个基本的注册 api 并使用 find() 函数检查是否有任何重复的 api 并且在 if() 条件下我抛出 BadRequestException 但如果输入电子邮件已在使用中它会给我一个错误。另一个项目中非常相似的代码没有给出任何错误,但这是错误的。
这是代码 sn-p。当给定一个已经在数据库中注册的电子邮件 ID 时,它应该抛出异常。
import { Injectable, BadRequestException } from '@nestjs/common';
import { UsersService } from './users.service';
import { randomBytes, scrypt as _scrypt } from 'crypto';
import { promisify } from 'util';
const scrypt = promisify(_scrypt);
@Injectable()
export class AuthService {
constructor(private usersService: UsersService) {}
async signup(ogname: string,
email: string,
password: string,
phone: string,
date_of_birth: Date,
type: string,) {
// See if email is in use
const name = null;
const users = await this.usersService.find(name, email);
console.log(users.length);
if (users.length) {
throw new BadRequestException('email in use');
}
// Hash the users password
// Generate a salt
const salt = randomBytes(8).toString('hex');
// Hash the salt and the password together
const hash = (await scrypt(password, salt, 32)) as Buffer;
// Join the hashed result and the salt together
const result = salt + '.' + hash.toString('hex');
//Create a new user and save it
const user = await this.usersService.create(ogname, email, result, phone, date_of_birth, type)
// return the user
return user;
}
signin() {}
}
预期结果:
{
"statusCode": 400,
"message": "email in use",
"error": "Bad Request"
}
这是整个代码的github链接: https://github.com/chaitanya2108/appaya_basketball_club
【问题讨论】:
标签: asynchronous exception async-await nestjs