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.

@ApiHideProperty() makes property read-only, TypeORM can't assign values to entity

See original GitHub issue

Bug report

Current behavior

When you decorate property of TypeORM entity with @ApiHideProperty(), you get following error as the property is read-only - Cannot assign to read only property 'property' of object '#<Entity>'.

Expected behavior

The property shouldn’t be read-only, so TypeORM can create class and assign values from db.

Minimal reproduction of the problem with instructions

Clone: https://github.com/nestjs/nest/tree/master/sample/05-sql-typeorm.

Install @nestjs/swagger (npm i @nestjs/swagger).

Decorate property, for example views in photo.entity.ts.

@Column('int')
@ApiHideProperty()
views: number;
Add a photo to database. You can edit `findAll` method in service to ensure that it returns at least one photo (or just add it manually).
  async findAll(): Promise<Photo[]> {
    await this.photoRepository.save({
      name: 'test',
      description: 'test',
      filename: 'test',
      views: 99,
      isPublished: true,
    });

    return this.photoRepository.find();
  }

Running the application and navigating to localhost:3000/photo causes an error - Cannot assign to read only property 'views' of object '#<Photo>'.

What is the motivation / use case for changing the behavior?

For example, you want to hide some internal properties, like createdAt.

Environment

Nest version: 6.10.14
nestjs/swagger version: 4.1.7
TypeORM driver: pg

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kamilmysliwieccommented, Jan 10, 2020

Fixed in 4.1.9. Thanks for reporting

0reactions
lock[bot]commented, Apr 25, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ApiHideProperty() makes property read-only, TypeORM can't assign ...
ApiHideProperty () makes property read-only, TypeORM can't assign values to entity.
Read more >
Nestjs Swagger CLI Plugin displays entities from relations ...
You can use the @ApiHideProperty() decorator on all relations of your TypeORM entity, that way it won't be loaded into Swagger-UI schemas.
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