Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
Override nestjs/crud response

I use for this project nest and the nestjs/crud library. Unfortunately I can't override the createOneBase function so that it returns a person response I looked for a solution in the last posts of the same topic. I would like to change the answer when I create a user.

what I have:

    {
        "id": 12,
        "username": "test",
        "password": "tests",
        "email": "test@foo.bar"
    }

what I expect:

    {
        "id": 12,
        "username": "test",
        "password": "tests",
        "email": "test@foo.bar",
        "token": "TOKEN"
    }

my controller:

import { Controller } from '@nestjs/common';
import { UsersService } from './users.service';
import { Crud, Override, ParsedRequest, ParsedBody, CrudRequest, CrudController } from '@nestjsx/crud';
import { User, UserDTO } from './user';

@Crud({
  model: {
    type: User,
  },
})
@Controller('users')
export class UsersController {
  constructor(public service: UsersService) {}

  get base(): CrudController<User | UserDTO> {
    return this;
  }

  @Override()
  createOne(
    @ParsedRequest() req: CrudRequest,
    @ParsedBody() dto: User,
  ): Promise<User | UserDTO> {
    const userDto: UserDTO = {
      id: dto.id,
      username: dto.username,
      password: dto.password,
      email: dto.email,
      token: 'TOKEN',
    };
    return this.base.createOneBase(req, userDto);
  }
}

my entities:

import { IsDefined, IsString, MinLength } from 'class-validator';
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  @IsDefined({ always: true })
  @IsString({ always: true })
  @MinLength(1, { always: true })
  username: string;

  @Column()
  @IsDefined({ always: true })
  @IsString({ always: true })
  @MinLength(5, { always: true })
  password: string;

  @Column()
  @IsDefined({ always: true })
  @IsString({ always: true })
  @MinLength(1, { always: true })
  email: string;
}

export class UserDTO {
  id: number;
  username: string;
  password: string;
  email: string;
  token: string;
  access_token?: string;
}

so I don't understand what's wrong with it, thanks for your help

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

To override a Nestjs/Crud method use the decorator @Override(functionName) Nestjs/Crud Doc

 @Override('createOneBase')
 createOne(
    @ParsedRequest() req: CrudRequest,
    @ParsedBody() dto: Hero) 
 {
    return this.base.createOneBase(req, dto);
 }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!