question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Class constructor UserDto cannot be invoked without 'new'

See original GitHub issue

First of all, this lib is awesome!

I am facing the same problem of https://github.com/nestjsx/crud/issues/378 Class constructor UserDto cannot be invoked without 'new' TypeError: Class constructor UserDto cannot be invoked without 'new' I modified the boilerplate https://github.com/NarHakobyan/awesome-nest-boilerplate adding the crud controller and crud service.

The main differences between your example and the implementation are: I use an AbstractDto with a constructor that receives an AbstractEntity. Here is the AbstractDto:

import { AbstractEntity } from '../abstract.entity';

export class AbstractDto {
  id: string;
  createdAt: Date;
  updatedAt: Date;

  constructor(entity: AbstractEntity) {
    this.id = entity.id;
    this.createdAt = entity.createdAt;
    this.updatedAt = entity.updatedAt;
  }
}

AbstractEntity:

import {
  CreateDateColumn,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';

import { UtilsService } from '../providers/utils.service';
import { AbstractDto } from './dto/AbstractDto';

export abstract class AbstractEntity<T extends AbstractDto = AbstractDto> {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @CreateDateColumn({
    type: 'timestamp without time zone',
    name: 'created_at',
  })
  createdAt: Date;

  @UpdateDateColumn({
    type: 'timestamp without time zone',
    name: 'updated_at',
  })
  updatedAt: Date;

  abstract dtoClass: new (entity: AbstractEntity, options?: any) => T;

  toDto(options?: any) {
    return UtilsService.toDto(this.dtoClass, this, options);
  }
}

And on top of that I create my dtos and entities: UserDto

import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';

import { RoleType } from '../../../common/constants/role-type';
import { AbstractDto } from '../../../common/dto/AbstractDto';
import { ProfileDto } from '../../profile/profile.dto';
import { UserEntity } from '../user.entity';

export class UserDto extends AbstractDto {
  @ApiPropertyOptional()
  firstName: string;

  @ApiPropertyOptional()
  lastName: string;

  @ApiPropertyOptional({ enum: RoleType })
  role: RoleType;

  @ApiPropertyOptional()
  email: string;

  constructor(user: UserEntity) {
    super(user);
    this.firstName = user.firstName;
    this.lastName = user.lastName;
    this.role = user.role;
    this.email = user.email;
  }
}

UserEntity

import { Column, Entity, Index } from 'typeorm';

import { AbstractEntity } from '../../common/abstract.entity';
import { RoleType } from '../../common/constants/role-type';
import { UserDto } from './dto/user.dto';
import { PasswordTransformer } from './password.transformer';

@Entity({ name: 'users' })
export class UserEntity extends AbstractEntity<UserDto> {
  @Column({ nullable: true })
  firstName: string;

  @Column({ nullable: true })
  lastName: string;

  @Column({ type: 'enum', enum: RoleType, default: RoleType.USER })
  role: RoleType;

  @Index('email')
  @Column({ unique: true, nullable: true })
  email: string;

  @Column({ nullable: true, transformer: new PasswordTransformer() })
  password: string;

  dtoClass = UserDto;
}

I found some descriptions indicating that I need to change the tsconfig.json target to at least ES6, but it is already there.

My option is to not use a dtos with constructors, right? Will I need to create another dto just for this scenario like the other dto but without constructor? Or could you provide an example using dtos with constructors?

Thanks in advance

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
valerybugakovcommented, Mar 16, 2020

adding @Exclude() to dtoClass helped me to resolve the issue:

  // src/common/abstract.entity.ts

  @Exclude()
  abstract dtoClass: new (entity: AbstractEntity, options?: any) => T
0reactions
Germano123commented, Nov 18, 2021

I’m using the updated version of awesome boilerplate. It’s incredible 😄

I’m facing a similar issue :S using the abstract entities and abstract dtos. Even updating the target to ES6 didn’t help :S The problem is: using typeorm update in an entity causes it to break, even using @Exclude() property in dtoClass variable in AbstractEntity it still is assuming the need of the dtoClass variable.

The update code should be similar to this:

async update(dataId: string, data: DataEntity): Promise<DataEntity> {
  await dataRepository.update(dataId, data); // the problem lies here in data
  return await dataRepository.find(dataId);
}

image

I’ve tried to break the solution in typeorm save method which is not the best solution, but still got the same error :S

The code should look like this.

async updateWithSave(dataId: string, data: DataEntity): Promise<DataEntity> {
  let dataEntity = await this.repo.find(dataId);
  dataEntity = { ...dataEntity, ...data, id: dataId, toDto: any }; // the problem lies here in data
  return await this.repo.save(dataEntity);
}

The error is: “EntityColumnNotFound: No entity column “dtoClass” was found.”

Read more comments on GitHub >

github_iconTop Results From Across the Web

Class constructor cannot be invoked without 'new' - typescript ...
I have class ChatRoom that extends Colyseus.Room . At run-time I get this error: Class constructor Room cannot be invoked without 'new'.
Read more >
Class constructor cannot be invoked without 'new' in TS
The "Class constructor cannot be invoked without new" error occurs when the target property in tsconfig.json is set to lower than es6 or...
Read more >
Javascript ES6 TypeError Class constructor Client cannot be ...
When I try to execute nodemon command I always see this error TypeError: Class constructor Client cannot be invoked without 'new'.
Read more >
Class constructor Positions cannot be invoked without 'new'
Class constructor Positions cannot be invoked without 'new'. Any suggestions? I think the problem is related to babel but I can't find an ......
Read more >
3 ways to check the object passed to mocks with Moq in C# ...
When writing unit tests, you can use Mocks to simulate the usage of class dependencies. Even though some developers are harshly against the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found