When I execute the following code I get "201 Created" response from the server but actually data is not inserted in the server.
I am using nestJS with TypeORM and Postgres for my application.
import { Repository, EntityRepository } from "typeorm";
import { User } from './user.entity';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';
import { ConflictException, InternalServerErrorException } from "@nestjs/common";
@EntityRepository(User)
export class UserRepository extends Repository<User>{
async signUp(authCredentialsDto: AuthCredentialsDto): Promise<void>{
const {username, password} = authCredentialsDto;
const user = new User();
user.username = username;
user.password = password;
try{
await user.save();
} catch (error) {
if (error.code === '23505'){ // error code for duplicate value in a col of the server
throw new ConflictException('Username already exists');
} else {
throw new InternalServerErrorException();
}
}
}
}
And I get following response in the VS Code terminal, instead of getting "201 Crated" from the server:
(node:14691) UnhandledPromiseRejectionWarning: Error: Username already exists
at UserRepository.signUp (/home/rajib/practicing coding/nestJS-projects/nestjs-task-management/dist/auth/user.repository.js:24:23)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:14691) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:14691) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
And the controller modules code is following:
import { Controller, Post, Body, ValidationPipe } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';
@Controller('auth')
export class AuthController {
constructor( private authService: AuthService){}
@Post('/signup')
signUp(@Body(ValidationPipe) authCredentialsDto: AuthCredentialsDto): Promise<void>{
return this.authService.signUp(authCredentialsDto);
}
}