Alexandr Boroshenko
Alexandr Boroshenko

Reputation: 71

NestJS - No response getById request

I need your help. I recently started learning the nestjs framework. I am currently trying to do CRUD operations without a database. I have 2 methods: getAllUsers and getDifferentUser. I manage to get all users, but I can't get a user by id. I don't get any error, 200 response, but my response is empty. What is my mistake? Thank you very much

userService.ts

public getAllUsers(): IUserModel[] {
   return this.users;
}

public findDifferentUser(id: number): IUserModel {
   return this.users.find(user => user.id === id);
}

user.controller.ts

@Controller('users')
export class UserController {

   constructor(private userService: UserService) {}

   @Get()
   public getAllUsers(): IUserModel[] {
      return this.userService.getAllUsers();
   }

   @Get(':id')
   public getDifferentUser(@Param('id') id: number): IUserModel {
      return this.userService.findDifferentUser(id);
   }
}

user.model.ts

export interface IUserModel {
   id: number;
   name: string;
   age: number;
   description: string;
   quality: string;
}

Upvotes: 1

Views: 691

Answers (1)

Huthaifa Muayyad
Huthaifa Muayyad

Reputation: 12383

Use this in your controller:

 @Get(':id')
   public getDifferentUser(@Param('id', ParseIntPipe) id: number): IUserModel {
      return this.userService.findDifferentUser(id);
   }

Because you need to convert id to a number. When you recieve it in param, it's a string.

  • As a general rule, for any requests, regardless of the programming language, parameters and queryParamaters are always strings.

  • You need to handle them in your code and convert them to the format you want. In your case, parse the value to an integer.

  • In NestJs, you can leverage the use of DTOs. And use them in your query parameters. Something like this:

  @Get('exams/:examId/query')
  async getAllExamData(
    @Param('examId', new ParseIntPipe()) examId: number,
    @Query() data: ExamQueryDto,
  ) { 
   // write your logic here to handle whatever you need
  }

And the ExamQueryDto will look similar to this, based on your use case:

import {  Type } from 'class-transformer';
import { IsBoolean, IsInt, IsOptional } from 'class-validator';

export class ExamQueryDto {
  @IsOptional()
  @IsInt()
  @Type(() => Number)
  userId?: number;

  @IsOptional()
  @IsInt()
  @Type(() => Number)
  examAttemptId?: number;

  @IsOptional()
  @IsBoolean()
  @Type(() => Boolean)
  reviewSubmissions: boolean;
}

Notice the use of class-transformer, which transforms the data recieved from the query, into the type you want. For example string -> number.

  • DTOs are great. Also make use of them in your @Body() when your request has a body. If you send the body in JSON format, the data type will be preserved and you wont need to convert it, but the DTO will help you pass these bits of data to other functions. And you can use it like this myAwesomeDto.userId for example.

Upvotes: 1

Related Questions