【问题标题】:I'm facing a n error when throwing a BadRequestException which is not supposed to happen我在抛出不应该发生的 BadRequestException 时遇到错误
【发布时间】: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"
}

Unexpected result:

这是整个代码的github链接: https://github.com/chaitanya2108/appaya_basketball_club

【问题讨论】:

    标签: asynchronous exception async-await nestjs


    【解决方案1】:

    我刚刚阅读了您在 GitHub 上发布的代码,它与您在此处发布的代码有很大不同,因此我无法重现该问题。

    但是,我可以给你一些更好的实施建议:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 2020-02-06
      • 2015-04-28
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      • 2019-11-09
      相关资源
      最近更新 更多