Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda