简体   繁体   English

覆盖 nestjs/crud 响应

[英]Override nestjs/crud response

I use for this project nest and the nestjs/crud library.我用于这个项目的 nest 和 nestjs/crud 库。 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.不幸的是,我无法覆盖 createOneBase function 以便它返回我在同一主题的最后一篇文章中寻找解决方案的人员响应。 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:我的 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所以我不明白它有什么问题,谢谢你的帮助

To override a Nestjs/Crud method use the decorator @Override(functionName) Nestjs/Crud Doc要覆盖 Nestjs/Crud 方法,请使用装饰器 @Override @Override(functionName) Nestjs/Crud Doc

 @Override('createOneBase')
 createOne(
    @ParsedRequest() req: CrudRequest,
    @ParsedBody() dto: Hero) 
 {
    return this.base.createOneBase(req, dto);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM