0

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);      
    }
}
Rajib Chanda
  • 33
  • 1
  • 5
  • Do you have somewhere in your code not `await`ing a response? Or are you using `res.send()` instead of returning a value from the `controller`? – Jay McDoniel Sep 06 '20 at 01:26
  • 2
    can you add your code of your controller? – critrange Sep 06 '20 at 03:19
  • @JayMcDoniel Thanks for your response. I have edited the post and added the **controller** code in my post if that helps you solving my problem. – Rajib Chanda Sep 07 '20 at 09:51
  • @yash Thanks for your response. I have edited the post and added the **controller** code in my post if that helps you solving my problem. – Rajib Chanda Sep 07 '20 at 09:52

1 Answers1

0

Good day.

I think the issue you have here is not awaiting the method call to the authService.signup() method in your AuthController...

So, the correction should be:

In the AuthService::signup() method:

return this.userRepository.signup(authCredentialsDto);

In the AuthController::signup() method:

return this.authService.signup(authCredentialsDto);
Kim Kern
  • 54,283
  • 17
  • 197
  • 195
Andromadus
  • 31
  • 3