[英]I'm facing a n error when throwing a BadRequestException which is not supposed to happen
I am writing a basic signup api and using find() function to check if there are any duplicate api and in an if() condition im throwing a BadRequestException but it is giving me an error if the input email is already in use.我正在编写一个基本注册 api 并使用 find() function 检查是否有任何重复的 api 并且在 if() 条件下我抛出 BadRequestException 但如果输入 email 已在使用中,它会给我一个错误。 A very similar code in another project is not giving any error but this is.另一个项目中非常相似的代码没有给出任何错误,但这是错误的。
This is the code snippet.这是代码片段。 when given an email id already registered in the database it is supposed to throw an exception.当给定一个已经在数据库中注册的 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() {}
}
expected result:预期结果:
{
"statusCode": 400,
"message": "email in use",
"error": "Bad Request"
}
here is the github link for the entire code: https://github.com/chaitanya2108/appaya_basketball_club这是整个代码的 github 链接: https://github.com/chaitanya2108/appaya_basketball_club
I've just read the code you posted on GitHub, it's quite different than the code you posted here, then I cannot reproduce the issue.我刚刚阅读了您在 GitHub 上发布的代码,它与您在此处发布的代码有很大不同,因此我无法重现该问题。
However, I can give you some suggestion for better implementations:但是,我可以给你一些更好的实施建议:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.