简体   繁体   English

我在抛出不应该发生的 BadRequestException 时遇到错误

[英]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"
}

Unexpected result:意想不到的结果:

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.

相关问题 我在将 express 连接到 Rabbit MQ 时遇到问题 - I'm facing problems connecting express to Rabbit MQ 异步模块错误处理,有没有办法我可以确定哪个回调块引发错误。 - async Module Error Handling , is there a way i can identify which block of callback is throwing error. 异步等待n事情发生 - Asynchronously wait for n things to happen 不使用异步功能时出现异步错误? - Getting an Async error when I'm not using an Async function? 当我使用带有 try/catch 的 rtk 查询时,我的错误对象未定义 - my error object is undefined when i`m using rtk query with try/catch 抛出异步方法时未捕获错误 - error is not being caught when throwing inside a async method 使用等待时请求承诺抛出意外的标识符错误 - request-promise throwing Unexpected identifier error when using await Dart Future:抛出Error时,主线程被中断,但在Flutter中不是 - Dart Future: when throwing Error, the the main thread is interrupted, but in Flutter it's not SqlCommand.StatementCompleted 应该什么时候触发? - When is SqlCommand.StatementCompleted supposed to fire? 从控制器调用异步/等待时,Windows Azure抛出DeadLock错误 - Windows Azure throwing DeadLock error when calling async/await from Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM