[英]I'm facing a n error when throwing a BadRequestException which is not supposed to happen
我正在編寫一個基本注冊 api 並使用 find() function 檢查是否有任何重復的 api 並且在 if() 條件下我拋出 BadRequestException 但如果輸入 email 已在使用中,它會給我一個錯誤。 另一個項目中非常相似的代碼沒有給出任何錯誤,但這是錯誤的。
這是代碼片段。 當給定一個已經在數據庫中注冊的 email 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
我剛剛閱讀了您在 GitHub 上發布的代碼,它與您在此處發布的代碼有很大不同,因此我無法重現該問題。
但是,我可以給你一些更好的實施建議:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.